redis 的基本操作

1. 使用jedis 连接redis数据库工具类

package com.umpay.utils;

import java.util.logging.Logger;

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

public class JedisPoolUtil 
{
      private Logger log = Logger.getLogger(this.getClass().getSimpleName());
      
	  private static volatile JedisPool jedisPool = null;
	  
	  private JedisPoolUtil() {}
	  
	  public static JedisPool getJedisPoolInstance()
	  {
	     if(null == jedisPool)
	     {
	       synchronized (JedisPoolUtil.class)
	       {
	          if(null == jedisPool)
	          {
	            JedisPoolConfig poolConfig = new JedisPoolConfig();
	            poolConfig.setMaxActive(1000);
	            poolConfig.setMaxIdle(32);
	            poolConfig.setMaxWait(100*1000);
	            poolConfig.setTestOnBorrow(true);
	            
	            jedisPool = new JedisPool(poolConfig,"192.168.1.37",6379);
	          }
	       }
	     }
	     return jedisPool;
	  }
	  
	  public static void release(JedisPool jedisPool,Jedis jedis)
	  {
	     if(null != jedis)
	     {
	       jedisPool.returnResourceObject(jedis);
	     }
	  }
	  
	  
}


2. 实现类序列化和反序列化的工具类

package com.umpay.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeUtil {
    
    public static byte[] serialize(Object object) {
        ObjectOutputStream oos = null;
         ByteArrayOutputStream baos = null;
         try {
              // 序列化
             baos = new ByteArrayOutputStream();
             oos = new ObjectOutputStream(baos);
             oos.writeObject(object);
              byte[] bytes = baos.toByteArray();
              return bytes;
        } catch (Exception e) {

        }
         return null;
  }

   public static Object unserialize( byte[] bytes) {
        ByteArrayInputStream bais = null;
         try {
              // 反序列化
             bais = new ByteArrayInputStream(bytes);
             ObjectInputStream ois = new ObjectInputStream(bais);
              return ois.readObject();
        } catch (Exception e) {

        }
         return null;
  }

}

3. 对redis K-V / HashSet的测试


package com.umpay.pojo;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

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

import com.umpay.utils.JedisPoolUtil;
import com.umpay.utils.SerializeUtil;

public class TestRedis {

    private Jedis jedis = null;

    @Test
    public void test11() {
        // 获取 Jedis连接池实例
        JedisPool jedisPool = JedisPoolUtil.getJedisPoolInstance();

        // 准备测试数据
        User user1 = new User("user01", "nan", "已登录");
        User user2 = new User("user02", "nv", "未登录");

        try {
            // 获取jedis客户端
            jedis = jedisPool.getResource();

            // 清空redis中所有数据
            jedis.flushAll();

            // hset 使用在存储对象过程中避免 序列化, 可以使用: key 属性名 value 进行存储
            jedis.hset(user1.getUsername(), "username", user1.getUsername());
            jedis.hset(user1.getUsername(), "state", user1.getState());

            // 根据key获取到所有的 属性名 和 属性值
            Map user01 = jedis.hgetAll(user1.getUsername());
            // 遍历所有的属性名和属性值
            Set> entrySet = user01.entrySet();
            for (Entry entry : entrySet) {
                String key = entry.getKey();
                String value = entry.getValue();
                System.out.println(key + " : " + value);
            }

            // 如果对应的key的值没有, 则map不是null , 而是size为0
            Map user02 = jedis.hgetAll(user2.getUsername());
            if (user02 == null || user02.size() == 0) {
                System.out.println("user02 is null");
            }
            // 使用序列化向 redis中存储对象
            this.set(user1, user1.getUsername());
            this.set(user2, user2.getUsername());
            // 取出时需要反序列化
            System.out.println(this.get(user1.getUsername()));
            System.out.println(this.get(user2.getUsername()));

            // 向redis中添加测试数据
            jedis.set("key01", "value01");
            jedis.set("key02", "value02");
            // 设置key02过期时间, 3秒
            jedis.expire("key02", 3);

            String key02 = jedis.get("key02");
            System.out.println("key02 : " + key02);
            // 进程阻塞3秒后在吃获取
            Thread.sleep(3000);
            // 获取到的值为null, 而不是空串
            System.out.println("Thread key02 : " + jedis.get("key02"));

            // 获取redis中所有key为key开头的键值对
            Set keys = jedis.keys("key*");
            for (String key : keys) {
                System.out.println(key);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JedisPoolUtil.release(jedisPool, jedis);
        }
    }

    /** set Object */
    public String set(Object object, String key) {
        return jedis.set(key.getBytes(), SerializeUtil.serialize(object));
    }

    /** get Object */
    public Object get(String key) {
        byte[] value = jedis.get(key.getBytes());
        return SerializeUtil.unserialize(value);
    }

    /** delete a key **/
    public boolean del(String key) {
        return jedis.del(key.getBytes()) > 0;
    }

}



你可能感兴趣的:(redis)