7、redis之使用spring集成commons-pool来操作常见数据类型

环境的搭建参见:http://www.cnblogs.com/yangzhilong/p/4729857.html

下面直接贴具体的测试代码:

  1 package com.yzl;
  2 
  3 import java.util.Arrays;
  4 import java.util.HashMap;
  5 import java.util.List;
  6 import java.util.Map;
  7 
  8 import org.junit.After;
  9 import org.junit.Before;
 10 import org.junit.Test;
 11 import org.springframework.context.ApplicationContext;
 12 import org.springframework.context.support.ClassPathXmlApplicationContext;
 13 import org.springframework.util.SerializationUtils;
 14 
 15 import redis.clients.jedis.Jedis;
 16 import redis.clients.jedis.JedisPool;
 17 
 18 /**
 19  * RedisApp的测试类
 20  *
 21  * @author yangzhilong
 22  * @see [相关类/方法](可选)
 23  * @since [产品/模块版本] (可选)
 24  */
 25 public class RedisApp4Test {
 26     private JedisPool pool;
 27     private ApplicationContext app;
 28     private Jedis jedis;
 29     
 30     @Before
 31     public void before(){
 32         app = new ClassPathXmlApplicationContext("spring-config.xml");
 33         pool = app.getBean(JedisPool.class);
 34         jedis = pool.getResource();
 35     }
 36     
 37     @Test
 38     public void test(){
 39         //string
 40         operationString();
 41         
 42         //map
 43         operationMap();
 44         
 45         //list
 46         operationList();
 47         
 48         //自定义对象
 49         operationMyObject();
 50     }
 51     
 52     /**
 53      * 
 54      * 功能描述: <br>
 55      * 操作String
 56      *
 57      * @see [相关类/方法](可选)
 58      * @since [产品/模块版本](可选)
 59      */
 60     private void operationString(){
 61         jedis.set("name", "hello");
 62         jedis.append("name", " redis");
 63         String value = jedis.get("name");
 64         System.out.println("get value :" + value);
 65     }
 66     
 67     /**
 68      * 
 69      * 功能描述: <br>
 70      * 操作Map
 71      *
 72      * @see [相关类/方法](可选)
 73      * @since [产品/模块版本](可选)
 74      */
 75     private void operationMap(){
 76         Map<String, String> map = new HashMap<String, String>();
 77         map.put("key1", "value1");
 78         map.put("key2", "value2");
 79         jedis.hmset("map", map);
 80         //这里可以一次读取多个key
 81         List<String> result = jedis.hmget("map", "key1" , "key2");
 82         for(String str : result){
 83             System.out.println("get value from map : " + str);
 84         }
 85     }
 86     
 87     /**
 88      * 
 89      * 功能描述: <br>
 90      * 操作List
 91      *
 92      * @see [相关类/方法](可选)
 93      * @since [产品/模块版本](可选)
 94      */
 95     private void operationList(){
 96         List<String> result = Arrays.asList("item1","item2");
 97         for(String str : result){
 98             jedis.lpush("list", str);
 99         }
100         jedis.lpush("list", "value3");
101         result = jedis.lrange("list", 0, -1);
102         for(String str : result){
103             System.out.println("get value from list : " + str);
104         }
105         jedis.lpop("list");
106         for(String str : result){
107             System.out.println("get value from list again : " + str);
108         }
109     }
110     
111     /**
112      * 
113      * 功能描述: <br>
114      * 操作自定义对象
115      *
116      * @see [相关类/方法](可选)
117      * @since [产品/模块版本](可选)
118      */
119     private void operationMyObject(){
120         Entity entity = new Entity();
121         entity.setName("test");
122         entity.setAge(20);
123         //保存
124         jedis.set("obj".getBytes(), SerializationUtils.serialize(entity));
125         //读取
126         byte[] bytes = jedis.get("obj".getBytes());
127         Entity ret = (Entity)SerializationUtils.deserialize(bytes);
128         System.out.println("读取到自定义的类的属性name的值为:" + ret.getName());
129     }
130     
131     @After
132     public void after(){
133         //释放连接
134         pool.returnResourceObject(jedis);
135         System.out.println("end~~~");
136     }
137 }

运行的结果:

 1 get value :hello redis
 2 get value from map : value1
 3 get value from map : value2
 4 get value from list : value3
 5 get value from list : item2
 6 get value from list : item1
 7 get value from list again : value3
 8 get value from list again : item2
 9 get value from list again : item1
10 读取到自定义的类的属性name的值为:test
11 end~~~

下一篇:8、redis之事务1-redis命令

原文地址:https://www.cnblogs.com/yangzhilong/p/5245282.html