redis和spring整合

目录结构

redis和spring整合_第1张图片

pom.xml


    
      redis.clients
      jedis
      2.1.0
    
    
      org.springframework
      spring-core
      4.3.8.RELEASE
    
    
      org.springframework
      spring-beans
      4.3.8.RELEASE
    
    
      org.springframework
      spring-tx
      4.3.8.RELEASE
    
    
      org.springframework
      spring-context
      4.3.8.RELEASE
    
    
      org.springframework
      spring-context-support
      4.3.8.RELEASE
    
    
      org.springframework
      spring-aop
      4.3.8.RELEASE
    
    
      org.springframework
      spring-aspects
      4.3.8.RELEASE
    
    
      commons-logging
      commons-logging
      1.1
    
    
      org.springframework
      spring-test
      4.3.8.RELEASE
    
    
      junit
      junit
      4.11
      test
    
  

redis.properties

#访问地址
redis.host=192.168.126.130
#访问端口
redis.port=6379  
#注意,如果没有password,此处不设置值,但这一项要保留
redis.pass=123456
#超时时间
redis.timeout=60
#使用哪个数据库
redis.default.db=0

#最大空闲时间,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。
redis.maxIdle=300  
#连接池的最大数据库连接数。设为0表示无限制
redis.maxActive=600  
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
redis.maxWait=1000  
#在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
redis.testOnBorrow=true 

applicationContext.xml




    

    
    
        
        
        
        
    
   
    
        
        
        
        
        
        
    
    
        
    

RedisUtil.java

package com.luheng.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class RedisUtil {
    private JedisPool jedisPool;

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    //添加
    public void set(String key, String value){
        Jedis jedis = this.jedisPool.getResource();
        jedis.set(key, value);
        this.jedisPool.returnResource(jedis);
    }

    //添加,带超时时间
    public void setex(String key, int seconds, String value){
        Jedis jedis = this.jedisPool.getResource();
        jedis.setex(key, seconds, value);
        this.jedisPool.returnResource(jedis);
    }

    //获取
    public String get(String key){
        Jedis jedis = this.jedisPool.getResource();
        String value = jedis.get(key);
        this.jedisPool.returnResource(jedis);
        return value;
    }

    //查看某个键是否存在
    public boolean exists(String key){
        Jedis jedis = this.jedisPool.getResource();
        Boolean exists = jedis.exists(key);
        this.jedisPool.returnResource(jedis);
        return exists;
    }

    //查看超时时间
    public Long ttl(String key){
        Jedis jedis = this.jedisPool.getResource();
        Long ttl = jedis.ttl(key);
        this.jedisPool.returnResource(jedis);
        return ttl;
    }

    //删除
    public void del(String key){
        Jedis jedis = this.jedisPool.getResource();
        jedis.del(key);
        this.jedisPool.returnResource(jedis);
    }
}

Test.java

package com.luheng.test;

import com.luheng.util.RedisUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        RedisUtil redisUtil = (RedisUtil)applicationContext.getBean("redisUtil");
        redisUtil.set("username", "fengqing");
        String username = redisUtil.get("username");
        System.out.println(username);
    }
}


你可能感兴趣的:(redis)