Table of Contents
The goal of this section" >The goal of this section
redis server" >redis server
Configuring jedis" >Configuring jedis
Test" >Test
Home Java javaTutorial Get Jedis examples via JedisPool

Get Jedis examples via JedisPool

Jun 29, 2017 am 10:00 AM
java redis

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:

Get Jedis examples via JedisPool

redis server

Run redis-server.exe locally, Then create a new jedis.properties in resources:

redis.host=localhost
redis.port=6379
Copy after login

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>
Copy after login

Test

Add a unit test and obtain the JedisPool instance object through the @Resource annotation.

@Resourceprivate JedisPool jedisPool;
Copy after login

Then call the getResource() method of the jedisPool object to obtain the Jedis instance.

Jedis jedis = jedisPool.getResource();
Copy after login

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);

    }
Copy after login

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>
Copy after login

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>
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

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's Role: Exploring the Data Storage and Management Capabilities Redis's Role: Exploring the Data Storage and Management Capabilities Apr 22, 2025 am 12:10 AM

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.

What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? Apr 19, 2025 pm 08:03 PM

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

Redis: Understanding Its Architecture and Purpose Redis: Understanding Its Architecture and Purpose Apr 26, 2025 am 12:11 AM

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? Why is the return value empty when using RedisTemplate for batch query? Apr 19, 2025 pm 10:15 PM

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...

Why does the redisTemplate.opsForList().leftPop() method not support passing in parameters to pop up multiple values ​​at once? Why does the redisTemplate.opsForList().leftPop() method not support passing in parameters to pop up multiple values ​​at once? Apr 19, 2025 pm 10:27 PM

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

In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? Apr 19, 2025 pm 10:57 PM

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

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

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.

See all articles