Skip to content

RedisManager

Jordan Samhi edited this page Aug 22, 2023 · 1 revision

RedisManager 🗄️

RedisManager is a utility class for managing connections to a Redis server and performing various operations on lists, sets, keys, and values in that server.

Table of Contents

Overview

RedisManager leverages the Jedis library to interact with a Redis server, providing a simplified and concise interface for performing common Redis operations.

Constructor

RedisManager(String server, String port, String auth)

Connects to the specified Redis server using given connection parameters.

List Operations

lpush(String list, String val)

Pushes a value onto a list.

lrange(String list, long start, long end)

Gets a range of elements from a list.

lrem(String list, long count, String value)

Removes the first count occurrences of elements equal to value from the list.

Set Operations

sadd(String set, String val)

Adds a member to a set.

spop(String set)

Removes and returns a random member from a set.

sismember(String set, String val)

Checks if a member is part of a set.

Key-Value Operations

get(String key)

Gets the value of the specified key.

set(String key, String value)

Sets the specified key to the specified value.

del(String... keys)

Deletes one or more keys.

Byte Array Operations

lpush(String list, byte[] val)

Pushes a byte array onto a list.

sadd(String set, byte[] val)

Adds a byte array to a set.

set(String key, byte[] value)

Sets the specified key to the specified byte array.

Examples

Example 1: Connecting to a Redis Server

RedisManager manager = new RedisManager("localhost", "6379", "password");

Example 2: List Operations

manager.lpush("myList", "value");
List<String> values = manager.lrange("myList", 0, -1);

Example 3: Set and Key-Value Operations

manager.sadd("mySet", "value");
String value = manager.get("key");

Example 4: Byte Array Operations

byte[] byteArray = "value".getBytes(StandardCharsets.UTF_8);
manager.lpush("myList", byteArray);

These examples showcase how to use RedisManager to perform various Redis operations.