![]() |
Redis is an open-source, in-memory data store that is particularly useful for applications that require high-speed data retrieval and low-latency access. In Java programs, you can interact with Redis using the Jedis library, a popular Java client for Redis. Redis Installation in JavaTo use Redis with Java programs, you need to install Redis on your system. Here’s a basic installation guide: 1. Download RedisYou can download the latest stable version of Redis from the official website or use a package manager if your operating system supports it. 2. Extract and CompileAfter downloading, extract the files and navigate to the extracted directory in your terminal. Run the following commands:
3. Start Redis ServerOnce compiled, you can start the Redis server by running:
4. Connecting to Redis Server from Java:To connect to the Redis server from your Java program, you’ll need to include the Jedis library in your project. You can add it to your project using Maven or Gradle, depending on your build tool. Here’s how you can create a connection: Java
Basic Redis Commands and ExamplesRedis supports various data types and operations. Here are some basic commands and examples using Jedis: 1. String Data TypeThe string data type in Redis is a simple key-value pair, where the value is a string. It’s useful for storing simple data like numbers, text, or serialized objects. Command: SET key value: Sets the value of a key.
This sets the value of the key “name” to “John”. The key-value pair is now stored in Redis. Java
Output: Name: John
2. Hash Data TypeA hash in Redis is a collection of field-value pairs, where each field is a unique identifier within the hash. Commands: HSET key field value: Sets the field in the hash stored at the key to the specified value.
In this example, we’re storing information about a user with ID 1. We set the name and age fields using HSET. The data is stored as a hash with the key “user:1”. Java
Output: User: Alice, Age: 25
3. List Data TypeA list in Redis is an ordered collection of values. Lists allow for fast insertion and retrieval of elements at the beginning, middle, or end of the list. Commands: LPUSH key value [value …]: Prepends one or multiple values to a list.
We use LPUSH to add fruits to the “fruits” list. The order will be: “orange”, “banana”, “apple” (since we used LPUSH, the last item specified is added first). Then, LRANGE retrieves all elements from the “fruits” list Java
Output: Fruits: [orange, banana, apple]
4. Set Data TypeA set in Redis is an unordered collection of unique values. Sets are useful for storing data that shouldn’t have duplicate entries. Commands:
Here, we use Java
Output : Tags: [programming, java, redis]
Note: These are just the basic commands for each data type in Redis. Redis provides a wide range of commands for more advanced data structures and operations, such as sorted sets, hyperloglogs, and more. Remember to have a running Redis server on “localhost” (or update the host as needed) before executing these examples. The output should match the provided examples based on the Redis operations performed. |
Reffered: https://www.geeksforgeeks.org
System Design |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |