Get Jedis examples via JedisPool
The goal of this section
Get Jedis examples through JedisPool and complete simple Key-value read and write operations on redis.
The complete code structure is as follows:
redis server
Run redis-server.exe locally, Then create a new jedis.properties in resources:
redis.host=localhost redis.port=6379
Configuring jedis
We put the jedis related configuration in a separate In Spring Config, create a new applicationContext-jedis.xml in the resources/spring directory.
<!-- 加载配置属性文件 --> <property-placeholder></property-placeholder> <bean> <property></property> <!-- 最大能够保持idel状态的对象数 --> <property></property> <!-- 最大分配的对象数 --> <property></property> <!-- 当调用borrow Object方法时,是否进行有效性检查 --> </bean> <bean> <constructor-arg></constructor-arg> <constructor-arg></constructor-arg> <constructor-arg></constructor-arg> </bean>
Test
Add a unit test and obtain the JedisPool instance object through the @Resource annotation.
@Resourceprivate JedisPool jedisPool;
Then call the getResource() method of the jedisPool object to obtain the Jedis instance.
Jedis jedis = jedisPool.getResource();
First test the most basic get and set operations
@Testpublic void TestRedis() { Jedis jedis = jedisPool.getResource(); String key = "a"; jedis.set(key, "111"); String data = jedis.get(key); System.out.println(data); }
Running result: 111
Test a list operation again:
@Testpublic void testList(){ Jedis jedis=jedisPool.getResource(); String key="articles"; jedis.lpush(key,"文章1"); jedis.lpush(key,"文章2"); jedis.lpush(key,"文章3"); List<string> articles=jedis.lrange(key,0,3);for(String article:articles){ System.out.println(article); } }</string>
Run result:
Article 3
Article 2
Article 1
Another collection operation:
@Testpublic void testSet(){ Jedis jedis=jedisPool.getResource(); String key="tags"; jedis.sadd(key,"宝马"); jedis.sadd(key,"豪车"); jedis.sadd(key,"SUV"); jedis.sadd(key,"SUV"); Set<string> tags=jedis.smembers(key);for(String tag:tags){ System.out.println(tag); } }</string>
Run result:
BMW
luxury car
SUV
Let’s get here first, SO EASY!!
The above is the detailed content of Get Jedis examples via JedisPool. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

Why is the return value empty when using RedisTemplate for batch query? When using RedisTemplate for batch query operations, you may encounter the returned results...

Regarding the reason why RedisTemplate.opsForList().leftPop() does not support passing numbers. When using Redis, many developers will encounter a problem: Why redisTempl...

The optimization solution for SpringBoot timing tasks in a multi-node environment is developing Spring...

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.
