使用Redis的jedis客户端整合spring

一、前期准备

1、首先在Linux服务器上安装了Redis的server端,安装详情,请看我这篇博客http://blog.csdn.net/qq_31634461/article/details/78650407,在本篇博客中我们将在使用jedis作为Client端远程连接并操作Redis。
2.去下载Jedis-2.7.2.jar开发包,推荐最新版本的
3.导入开发包到工程

二、向pom文件中添加redis依赖

    
            <dependency>
                <groupId>redis.clientsgroupId>
                <artifactId>jedisartifactId>
                    <version>2.7.2version>
            dependency>

三、单机版测试

1、在spring的配置文件(applicationContext.xml)中加入:


    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        
        <property name="maxTotal" value="30" />
        
        <property name="maxIdle" value="10" />
        
        <property name="numTestsPerEvictionRun" value="1024" />
        
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        
        <property name="maxWaitMillis" value="1500" />
        
        <property name="testOnBorrow" value="true" />
        
        <property name="testWhileIdle" value="true" />
        
        <property name="blockWhenExhausted" value="false" />
    bean> 
    
    <bean id="redisClient" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="127.0.0.1">constructor-arg>
        <constructor-arg name="port" value="6379">constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig">constructor-arg>
    bean>

2、java代码

/**
     * 单机版测试
     */
    @Test
    public void testSpringJedisSingle() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
        Jedis jedis = pool.getResource();
        String string = jedis.get("key1");
        System.out.println(string);
        jedis.close();
        pool.close();
    }

四、集群版本

1.向配置文件中加入

<bean id="redisClient" class="redis.clients.jedis.JedisCluster">
        <constructor-arg name="nodes">
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="127.0.0.1">constructor-arg>
                    <constructor-arg name="port" value="7001">constructor-arg>
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="127.0.0.1">constructor-arg>
                    <constructor-arg name="port" value="7002">constructor-arg>
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="127.0.0.1">constructor-arg>
                    <constructor-arg name="port" value="7003">constructor-arg>
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="127.0.0.1">constructor-arg>
                    <constructor-arg name="port" value="7004">constructor-arg>
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="127.0.0.1">constructor-arg>
                    <constructor-arg name="port" value="7005">constructor-arg>
                bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="127.0.0.1">constructor-arg>
                    <constructor-arg name="port" value="7006">constructor-arg>
                bean>
            set>
        constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig">constructor-arg>
    bean>

2、java 测试代码

@Test
    public void testSpringJedisCluster() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        JedisCluster jedisCluster =  (JedisCluster) applicationContext.getBean("redisClient");
        String string = jedisCluster.get("key1");
        System.out.println(string);
        jedisCluster.close();
    }

五、jedis的Dao层

1、单机版Dao

public class JedisClientSingle implements JedisClient{

    @Autowired
    private JedisPool jedisPool; 

    @Override
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.get(key);
        jedis.close();
        return string;
    }

    @Override
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.set(key, value);
        jedis.close();
        return string;
    }

    @Override
    public String hget(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.hget(hkey, key);
        jedis.close();
        return string;
    }

    @Override
    public long hset(String hkey, String key, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(hkey, key, value);
        jedis.close();
        return result;
    }

    @Override
    public long incr(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    @Override
    public long expire(String key, int second) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.expire(key, second);
        jedis.close();
        return result;
    }

    @Override
    public long ttl(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

}

在Spring配置文件中加入

id="jedisClient" class="com.test.redis.dao.JedisClientSingle">

2、集群版Dao

public class JedisClientCluster implements JedisClient {

    @Autowired
    private JedisCluster jedisCluster;

    @Override
    public String get(String key) {
        return jedisCluster.get(key);
    }

    @Override
    public String set(String key, String value) {
        return jedisCluster.set(key, value);
    }

    @Override
    public String hget(String hkey, String key) {
        return jedisCluster.hget(hkey, key);
    }

    @Override
    public long hset(String hkey, String key, String value) {
        return jedisCluster.hset(hkey, key, value);
    }

    @Override
    public long incr(String key) {
        return jedisCluster.incr(key);
    }

    @Override
    public long expire(String key, int second) {
        return jedisCluster.expire(key, second);
    }

    @Override
    public long ttl(String key) {
        return jedisCluster.ttl(key);
    }

}

六、向缓存中添加业务

1、缓存的添加,注意添加时,不要影响原来的业务逻辑

//---------------------------------------------缓存添加--------------------     
        //从缓存中取内容
        try{
            String result=jedisClient.hget("test", id+"");
            if(!StringUtils.isBlank(result)){
                List list=JsonUtils.jsonToList(result, Test.class);
                return list;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
//----------------------------------------------------------------- 

2、缓存存储

//---------------------------------------------缓存存储--------------------     
                //从缓存中添加内容
                try{
                    //把list转换成字符串
                    String cacheString=JsonUtils.objectToJson(list);
                    jedisClient.hset("test",id+"", cacheString);

                }catch(Exception e){
                    e.printStackTrace();
                }
//----

七、缓存的同步

待续

你可能感兴趣的:(Linux服务器)