7、redis之使用spring集成commons-pool来操作常见数据类型
生活随笔
收集整理的這篇文章主要介紹了
7、redis之使用spring集成commons-pool来操作常见数据类型
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
環(huán)境的搭建參見(jiàn):http://www.cnblogs.com/yangzhilong/p/4729857.html
下面直接貼具體的測(cè)試代碼:
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的測(cè)試類(lèi) 20 * 21 * @author yangzhilong 22 * @see [相關(guān)類(lèi)/方法](可選) 23 * @since [產(chǎn)品/模塊版本] (可選) 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 //自定義對(duì)象 49 operationMyObject(); 50 } 51 52 /** 53 * 54 * 功能描述: <br> 55 * 操作String 56 * 57 * @see [相關(guān)類(lèi)/方法](可選) 58 * @since [產(chǎn)品/模塊版本](可選) 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 [相關(guān)類(lèi)/方法](可選) 73 * @since [產(chǎn)品/模塊版本](可選) 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 //這里可以一次讀取多個(gè)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 [相關(guān)類(lèi)/方法](可選) 93 * @since [產(chǎn)品/模塊版本](可選) 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 * 操作自定義對(duì)象 115 * 116 * @see [相關(guān)類(lèi)/方法](可選) 117 * @since [產(chǎn)品/模塊版本](可選) 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("讀取到自定義的類(lèi)的屬性name的值為:" + ret.getName()); 129 } 130 131 @After 132 public void after(){ 133 //釋放連接 134 pool.returnResourceObject(jedis); 135 System.out.println("end~~~"); 136 } 137 }運(yùn)行的結(jié)果:
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 讀取到自定義的類(lèi)的屬性name的值為:test 11 end~~~?
下一篇:8、redis之事務(wù)1-redis命令
轉(zhuǎn)載于:https://www.cnblogs.com/yangzhilong/p/5245282.html
總結(jié)
以上是生活随笔為你收集整理的7、redis之使用spring集成commons-pool来操作常见数据类型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SQL 编程
- 下一篇: Linux内核分析:完成一个简单的时间片