What is Jedis and why is it needed?
\n\nJedis is a minimalist, lightweight, and very popular Java library for working with Redis (Remote Dictionary Server). Redis is a high-performance in-memory data store that is often used as a database, cache, and message broker. Jedis provides a simple and intuitive API for interacting with a Redis server from Java applications.
\n\nWhy is Jedis needed? If your Java application uses Redis for caching, session management, task queues, or storing temporary data, Jedis is the most direct and efficient way to connect to Redis. It supports all major Redis data structures (strings, lists, sets, hashes, sorted sets), as well as features such as Pub/Sub, transactions, and Lua scripts.
\n\nJedis is known for its simplicity and lack of unnecessary dependencies. Unlike heavier clients (e.g., Lettuce), Jedis does not require connection pool configuration out of the box (although it supports it), making it an ideal choice for small and medium-sized projects where simplicity and development speed are important.
\n\nInstalling Jedis
\n\nJedis is distributed via Maven Central. To add the library to your project, you need to add a dependency to the pom.xml file (for Maven) or build.gradle (for Gradle).
Installation via Maven
\nAdd the following block to your pom.xml:
<dependency>\n <groupId>redis.clients</groupId>\n <artifactId>jedis</artifactId>\n <version>5.1.0</version> <!-- Use the latest stable version -->\n</dependency>\n\nInstallation via Gradle
\nAdd the line to your build.gradle:
implementation 'redis.clients:jedis:5.1.0'\n\nImportant: Version 5.x is the latest major update of Jedis, which includes support for Redis 7.x and new features. It is recommended to always use the latest stable version to get all fixes and new features.
\n\nQuick start with a code example
\n\nLet's write a simple program that connects to Redis, saves a value, and reads it back.
\n\nAssume the Redis server is running locally on the default port 6379.
\n\nimport redis.clients.jedis.Jedis;\n\npublic class JedisQuickStart {\n public static void main(String[] args) {\n // 1. Create a connection to Redis\n // Default is localhost:6379\n try (Jedis jedis = new Jedis("localhost", 6379)) {\n \n // 2. Check the connection (PING)\n String response = jedis.ping();\n System.out.println("Ping response: " + response); // Should be "PONG"\n\n // 3. Save a value\n jedis.set("user:1000:name", "Alice");\n jedis.set("user:1000:age", "30");\n\n // 4. Read a value\n String name = jedis.get("user:1000:name");\n String age = jedis.get("user:1000:age");\n\n System.out.println("User name: " + name);\n System.out.println("User age: " + age);\n\n // 5. Check if a key exists\n boolean exists = jedis.exists("user:1000:name");\n System.out.println("Key exists: " + exists);\n\n // 6. Delete a key\n long deleted = jedis.del("user:1000:age");\n System.out.println("Deleted keys: " + deleted);\n }\n }\n}\n\nThis example demonstrates basic operations: connection, writing, reading, checking existence, and deletion. Note the use of try-with-resources — this ensures the connection is closed after use.
Main Jedis methods
\n\nJedis provides hundreds of methods covering all Redis capabilities. Below are the 10 most commonly us