初用redis缓存

阅读更多

srping 集成 redis

pom文件:

 


    4.0.0
    redis
    redis
    0.0.1-SNAPSHOT
    
    
    
        
            org.springframework.data
            spring-data-redis
            1.0.2.RELEASE
        
    

 

spring 配置文件代码:












 或者这样将spring和redis集成

spring 配置文件:

  
  
  
      
  
      
          
          
          
          
      
      
      
      
      
          
             
      
       
  

 redis.properties:

# Redis settings  
redis.host=localhost  
redis.port=6379  
redis.pass=java2000_wl  
  
  
redis.maxIdle=300  
redis.maxActive=600  
redis.maxWait=1000  
redis.testOnBorrow=true

 redisService:

package com.test.redis;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;publicclassRedisService{/**
     * 通过key删除(字节)
     * @param key
     */publicvoiddel(byte[] key){this.getJedis().del(key);}/**
     * 通过key删除
     * @param key
     */publicvoiddel(String key){this.getJedis().del(key);}/**
     * 添加key value 并且设置存活时间(byte)
     * @param key
     * @param value
     * @param liveTime
     */publicvoidset(byte[] key,byte[] value,int liveTime){this.set(key, value);this.getJedis().expire(key, liveTime);}/**
     * 添加key value 并且设置存活时间
     * @param key
     * @param value
     * @param liveTime
     */publicvoidset(String key,String value,int liveTime){this.set(key, value);this.getJedis().expire(key, liveTime);}/**
     * 添加key value
     * @param key
     * @param value
     */publicvoidset(String key,String value){this.getJedis().set(key, value);}/**添加key value (字节)(序列化)
     * @param key
     * @param value
     */publicvoidset(byte[] key,byte[] value){this.getJedis().set(key, value);}/**
     * 获取redis value (String)
     * @param key
     * @return
     */publicStringget(String key){String value =this.getJedis().get(key);return value;}/**
     * 获取redis value (byte [] )(反序列化)
     * @param key
     * @return
     */publicbyte[]get(byte[] key){returnthis.getJedis().get(key);}/**
     * 通过正则匹配keys
     * @param pattern
     * @return
     */publicSet<String> keys(String pattern){returnthis.getJedis().keys(pattern);}/**
     * 检查key是否已经存在
     * @param key
     * @return
     */publicboolean exists(String key){returnthis.getJedis().exists(key);}/**
     * 清空redis 所有数据
     * @return
     */publicString flushDB(){returnthis.getJedis().flushDB();}/**
     * 查看redis里有多少数据
     */publiclong dbSize(){returnthis.getJedis().dbSize();}/**
     * 检查是否连接成功
     * @return
     */publicString ping(){returnthis.getJedis().ping();}/**
     * 获取一个jedis 客户端
     * @return
     */privateJedis getJedis(){if(jedis ==null){return jedisConnectionFactory.getShardInfo().createResource();}return jedis;}privateRedisService(){}//操作redis客户端privatestaticJedis jedis;@Autowired@Qualifier("jedisConnectionFactory")privateJedisConnectionFactory jedisConnectionFactory;} 
public static void main(String[] args) throws InterruptedException {
        ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
        //这里已经配置好,属于一个redis的服务接口
        RedisService redisService = (RedisService) app.getBean("redisService");

        String ping = redisService.ping();//测试是否连接成功,连接成功输出PONG
        System.out.println(ping);

        //首先,我们看下redis服务里是否有数据
        long dbSizeStart = redisService.dbSize();
        System.out.println(dbSizeStart);

        redisService.set("username", "oyhk");//设值(查看了源代码,默认存活时间30分钟)
        String username = redisService.get("username");//取值 
        System.out.println(username);
        redisService.set("username1", "oyhk1", 1);//设值,并且设置数据的存活时间(这里以秒为单位)
        String username1 = redisService.get("username1");
        System.out.println(username1);
        Thread.sleep(2000);//我睡眠一会,再去取,这个时间超过了,他的存活时间
        String liveUsername1 = redisService.get("username1");
        System.out.println(liveUsername1);//输出null

        //是否存在
        boolean exist = redisService.exists("username");
        System.out.println(exist);

        //查看keys
        Set keys = redisService.keys("*");//这里查看所有的keys
        System.out.println(keys);//只有username username1(已经清空了)

        //删除
        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);//如果为null,那么就是删除数据了

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

        //清空reids所有数据
        //redisService.flushDB();
    }

你可能感兴趣的:(redis)