Spring combined with redis

Spring combined with redis

 

core part

1. jedis, the client class of java to operate redis

2. spring-data-redis, providing the following jedisConnectionFactory

2. jedisConnectionFactory, used to link redis, configure the parameters of connecting redis

 

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>redis</groupId>
    <artifactId>redis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.0.2.RELEASE</version>
        </dependency>
    </dependencies>
    <dependency>  
        <groupId>redis.clients</groupId>  
        <artifactId>jedis</artifactId>  
        <version>2.1.0</version>  
    </dependency>
</project>

 

 

spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<!--Annotation description-->
<context:annotation-config />
<!-- Convert the class marked with the @Controller annotation to a bean -->
<context:component-scan base-package="com.mkfree.**" />
<!-- redis factory-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
    p:host-name="192.168.9.140" p:port="6379" p:password="87980879" />
<!-- redis service package -->
<bean id="redisService" class="com.mkfree.redis.test.RedisService">
</bean>

 

A redis client integrated with spring is configured here. The ip port password is determined by yourself. Here I have defined the required password authentication in the redis configuration file. Don't use it yet, for the sake of simplicity.

 

 

RedisService

It contains methods for redis, and there are more methods that have not been used. Here is a small example for getting started.

package com.mkfree.redis.test;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import redis.clients.jedis.Jedis;

/**
 * Encapsulate the redis cache server service interface
 * @author hk
 *
 * 2012-12-16 3:09:18 am
 */
public class RedisService {

    /**
     * delete by key (bytes)
     * @param key
     */
    public void del(byte [] key){
        this.getJedis().del(key);
    }
    /**
     * delete by key
     * @param key
     */
    public void del(String key){
        this.getJedis().del(key);
    }

    /**
     * Add key value and set lifetime (byte)
     * @param key
     * @param value
     * @param liveTime
     */
    public void set(byte [] key,byte [] value,int liveTime){
        this.set(key, value);
        this.getJedis().expire(key, liveTime);
    }
    /**
     * Add key value and set lifetime
     * @param key
     * @param value
     * @param liveTime
     */
    public void set(String key,String value,int liveTime){
        this.set(key, value);
        this.getJedis().expire(key, liveTime);
    }
    /**
     * add key value
     * @param key
     * @param value
     */
    public void set(String key,String value){
        this.getJedis().set(key, value);
    }
    /**Add key value (bytes) (serialize)
     * @param key
     * @param value
     */
    public void set(byte [] key,byte [] value){
        this.getJedis().set(key, value);
    }
    /**
     * Get redis value (String)
     * @param key
     * @return
     */
    public String get(String key){
        String value = this.getJedis().get(key);
        return value;
    }
    /**
     * Get redis value (byte [] ) (deserialize)
     * @param key
     * @return
     */
    public byte[] get(byte [] key){
        return this.getJedis().get(key);
    }

    /**
     * Match keys by regular
     * @param pattern
     * @return
     */
    public Set<String> keys(String pattern){
        return this.getJedis().keys(pattern);
    }

    /**
     * Check if key already exists
     * @param key
     * @return
     */
    public boolean exists(String key){
        return this.getJedis().exists(key);
    }
    /**
     * Clear all redis data
     * @return
     */
    public String flushDB(){
        return this.getJedis().flushDB();
    }
    /**
     * Check how much data there is in redis
     */
    public long dbSize(){
        return this.getJedis().dbSize();
    }
    /**
     * Check if the connection is successful
     * @return
     */
    public String ping(){
        return this.getJedis().ping();
    }
    /**
     * Get a jedis client
     * @return
     */
    private Jedis getJedis(){
        if(jedis == null){
            return jedisConnectionFactory.getShardInfo().createResource();
        }
        return jedis;
    }
    private RedisService (){

    }
    //Operate redis client
    private static Jedis jedis;
    @Autowired
    @Qualifier("jedisConnectionFactory")
    private JedisConnectionFactory jedisConnectionFactory;
}

 

 

Test code TestRedis.Java

package com.mkfree.redis.test;

import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * redis spring simple example
 * @author hk
 *
 * 2012-12-22 10:40:15 am
 */
public class TestRedis {

    public static void main(String[] args) throws InterruptedException {
        ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
        //It has been configured here and belongs to a redis service interface
        RedisService redisService = (RedisService) app.getBean("redisService");

        String ping = redisService.ping();//Test whether the connection is successful, the connection is successful and output PONG
        System.out.println(ping);

        //First, let's see if there is data in the redis service
        long dbSizeStart = redisService.dbSize();
        System.out.println(dbSizeStart);

        redisService.set("username", "oyhk");//Set the value (checked the source code, the default survival time is 30 minutes)
        String username = redisService.get("username");//取值
        System.out.println(username);
        redisService.set("username1", "oyhk1", 1);//Set the value, and set the survival time of the data (here in seconds)
        String username1 = redisService.get("username1");
        System.out.println(username1);
        Thread.sleep(2000);//I sleep for a while, and then go to fetch, this time exceeds, his survival time
        String liveUsername1 = redisService.get("username1");
        System.out.println(liveUsername1);//Output null

        //does it exist
        boolean exist = redisService.exists("username");
        System.out.println(exist);

        // view keys
        Set<String> keys = redisService.keys("*");//View all keys here
        System.out.println(keys);//Only username username1 (already cleared)

        //delete
        redisService.set("username2", "oyhk2");
        String username2 = redisService.get("username2");
        System.out.println(username2);
        redisService.del("username2");
        String username2_2 = redisService.get("username2");
        System.out.println(username2_2);//If it is null, then the data is deleted

        //dbsize
        long dbSizeEnd = redisService.dbSize();
        System.out.println(dbSizeEnd);

        //Clear all data of reids
        //redisService.flushDB();
    }
}

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326498632&siteId=291194637