分享

Redis实战之征服 Redis + Jedis + Spring (二)

tntzbzc 发表于 2014-11-15 13:22:05 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 4 18799
本帖最后由 howtodown 于 2014-11-15 13:45 编辑
接上一篇,
Redis实战之征服 Redis + Jedis + Spring (一)

扩充User属性:
  1. public class User implements Serializable {  
  2.     private static final long serialVersionUID = -1267719235225203410L;  
  3.   
  4.     private String uid;  
  5.   
  6.     private String address;  
  7.   
  8.     private String mobile;  
  9.   
  10.     private String postCode;  
  11. }
复制代码


  1. public class User implements Serializable {  
  2.     private static final long serialVersionUID = -1267719235225203410L;  
  3.   
  4.     private String uid;  
  5.   
  6.     private String address;  
  7.   
  8.     private String mobile;  
  9.   
  10.     private String postCode;  
  11. }  
复制代码






我期望的是:


  1. redis 127.0.0.1:6379> hmget uc.user.info.uid.u123456 address mobile postCode
  2. 1) "\xe4\xb8\x8a\xe6\xb5\xb7"
  3. 2) "13800138000"
  4. 3) "100859"
复制代码





几乎就是一个对象了!
但是,接下来的代码实现,让我彻底崩溃了!



二、代码实现1.保存——HMSET
  1. @Override  
  2. public void save(final User user) {  
  3.     redisTemplate.execute(new RedisCallback<Object>() {  
  4.         @Override  
  5.         public Object doInRedis(RedisConnection connection)  
  6.                 throws DataAccessException {  
  7.             byte[] key = redisTemplate.getStringSerializer().serialize(  
  8.                     "uc.user.info.uid." + user.getUid());  
  9.             BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate  
  10.                     .boundHashOps(key);  
  11.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  12.                     .serialize("mobile"), redisTemplate  
  13.                     .getStringSerializer().serialize(user.getMobile()));  
  14.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  15.                     .serialize("address"), redisTemplate  
  16.                     .getStringSerializer().serialize(user.getAddress()));  
  17.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  18.                     .serialize("postCode"), redisTemplate  
  19.                     .getStringSerializer().serialize(user.getPostCode()));  
  20.             connection.hMSet(key, boundHashOperations.entries());  
  21.             return null;  
  22.         }  
  23.     });  
  24. }
复制代码


  1. @Override  
  2. public void save(final User user) {  
  3.     redisTemplate.execute(new RedisCallback<Object>() {  
  4.         @Override  
  5.         public Object doInRedis(RedisConnection connection)  
  6.                 throws DataAccessException {  
  7.             byte[] key = redisTemplate.getStringSerializer().serialize(  
  8.                     "uc.user.info.uid." + user.getUid());  
  9.             BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate  
  10.                     .boundHashOps(key);  
  11.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  12.                     .serialize("mobile"), redisTemplate  
  13.                     .getStringSerializer().serialize(user.getMobile()));  
  14.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  15.                     .serialize("address"), redisTemplate  
  16.                     .getStringSerializer().serialize(user.getAddress()));  
  17.             boundHashOperations.put(redisTemplate.getStringSerializer()  
  18.                     .serialize("postCode"), redisTemplate  
  19.                     .getStringSerializer().serialize(user.getPostCode()));  
  20.             connection.hMSet(key, boundHashOperations.entries());  
  21.             return null;  
  22.         }  
  23.     });  
  24. }  
复制代码









这里用到:
  1. BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate.boundHashOps(key);  
  2. boundHashOperations.put(redisTemplate.getStringSerializer().serialize("mobile"), redisTemplate.getStringSerializer().serialize(user.getMobile()));  
复制代码

  1. BoundHashOperations<Serializable, byte[], byte[]> boundHashOperations = redisTemplate.boundHashOps(key);  
  2. boundHashOperations.put(redisTemplate.getStringSerializer().serialize("mobile"), redisTemplate.getStringSerializer().serialize(user.getMobile()));  
复制代码




看着就有点肿。。。Map封装完以后,用HMSET命令:
  1. connection.hMSet(key, boundHashOperations.entries());  
复制代码

  1. connection.hMSet(key, boundHashOperations.entries());  
复制代码





这时候就完成了哈希表的保存操作,可以在控制台看到相应的数据了。

  1. redis 127.0.0.1:6379> hmget uc.user.info.uid.u123456 address mobile postCode
  2. 1) "\xe4\xb8\x8a\xe6\xb5\xb7"
  3. 2) "13800138000"
  4. 3) "100859"
复制代码




2.获取——HMGET这一刻,我彻底崩溃了!取出来的值是个List,还得根据取得顺序,逐个反序列化,得到内容。

  1. @Override  
  2. public User read(final String uid) {  
  3.     return redisTemplate.execute(new RedisCallback<User>() {  
  4.         @Override  
  5.         public User doInRedis(RedisConnection connection)  
  6.                 throws DataAccessException {  
  7.             byte[] key = redisTemplate.getStringSerializer().serialize(  
  8.                     "uc.user.info.uid." + uid);  
  9.             if (connection.exists(key)) {  
  10.                 List<byte[]> value = connection.hMGet(  
  11.                         key,  
  12.                         redisTemplate.getStringSerializer().serialize(  
  13.                                 "address"),  
  14.                         redisTemplate.getStringSerializer().serialize(  
  15.                                 "mobile"), redisTemplate  
  16.                                 .getStringSerializer()  
  17.                                 .serialize("postCode"));  
  18.                 User user = new User();  
  19.                 String address = redisTemplate.getStringSerializer()  
  20.                         .deserialize(value.get(0));  
  21.                 user.setAddress(address);  
  22.                 String mobile = redisTemplate.getStringSerializer()  
  23.                         .deserialize(value.get(1));  
  24.                 user.setMobile(mobile);  
  25.                 String postCode = redisTemplate.getStringSerializer()  
  26.                         .deserialize(value.get(2));  
  27.                 user.setPostCode(postCode);  
  28.                 user.setUid(uid);  
  29.   
  30.                 return user;  
  31.             }  
  32.             return null;  
  33.         }  
  34.     });  
  35. }  
复制代码


  1. @Override  
  2. public User read(final String uid) {  
  3.     return redisTemplate.execute(new RedisCallback<User>() {  
  4.         @Override  
  5.         public User doInRedis(RedisConnection connection)  
  6.                 throws DataAccessException {  
  7.             byte[] key = redisTemplate.getStringSerializer().serialize(  
  8.                     "uc.user.info.uid." + uid);  
  9.             if (connection.exists(key)) {  
  10.                 List<byte[]> value = connection.hMGet(  
  11.                         key,  
  12.                         redisTemplate.getStringSerializer().serialize(  
  13.                                 "address"),  
  14.                         redisTemplate.getStringSerializer().serialize(  
  15.                                 "mobile"), redisTemplate  
  16.                                 .getStringSerializer()  
  17.                                 .serialize("postCode"));  
  18.                 User user = new User();  
  19.                 String address = redisTemplate.getStringSerializer()  
  20.                         .deserialize(value.get(0));  
  21.                 user.setAddress(address);  
  22.                 String mobile = redisTemplate.getStringSerializer()  
  23.                         .deserialize(value.get(1));  
  24.                 user.setMobile(mobile);  
  25.                 String postCode = redisTemplate.getStringSerializer()  
  26.                         .deserialize(value.get(2));  
  27.                 user.setPostCode(postCode);  
  28.                 user.setUid(uid);  
  29.   
  30.                 return user;  
  31.             }  
  32.             return null;  
  33.         }  
  34.     });  
  35. }  
复制代码






这个实现,跟Redis的命令几乎一模一样,指定Key,指定field,获取其值。

  1. List<byte[]> value = connection.hMGet(key,redisTemplate.getStringSerializer().serialize("address"),  
  2. redisTemplate.getStringSerializer().serialize("mobile"),   
  3. redisTemplate.getStringSerializer().serialize("postCode"));  
复制代码


  1. List<byte[]> value = connection.hMGet(key,redisTemplate.getStringSerializer().serialize("address"),  
  2. redisTemplate.getStringSerializer().serialize("mobile"),   
  3. redisTemplate.getStringSerializer().serialize("postCode"));  
复制代码







我绝对相信,要么是我用的过于肤浅,低估了Spring的封装能力。或者,我该直接Json!等等,这不是MongoDB干的事情吗?!
PS:这两篇博客里操作的数据类型,只能是String类型,还没搞定除此以外任何类型。吾将上下而求索~~~
上述操作也许你吐了,接下来的代码,就再吐一次吧!
封装对象的时候,一定要记得次序。。。。这绝对不是一个优质代码的实现风格!

  1. User user = new User();  
  2. String address = redisTemplate.getStringSerializer().deserialize(value.get(0));  
  3. user.setAddress(address);  
  4. String mobile = redisTemplate.getStringSerializer().deserialize(value.get(1));  
  5. user.setMobile(mobile);  
  6. String postCode = redisTemplate.getStringSerializer().deserialize(value.get(2));  
  7. user.setPostCode(postCode);  
复制代码
  1. User user = new User();  
  2. String address = redisTemplate.getStringSerializer().deserialize(value.get(0));  
  3. user.setAddress(address);  
  4. String mobile = redisTemplate.getStringSerializer().deserialize(value.get(1));  
  5. user.setMobile(mobile);  
  6. String postCode = redisTemplate.getStringSerializer().deserialize(value.get(2));  
  7. user.setPostCode(postCode);  
复制代码





好吧!苦逼的事情,就此结束。目标Json支持!




已有(4)人评论

跳转到指定楼层
howtodown 发表于 2014-11-15 13:47:42


继续补充:


为了简便操作,我使用了StringRedisTemplate。用字符串操作做展示。当然,你可以继续使用RedisTemplate。
闲言少叙,上代码,一目了然:

  1. /**
  2. * Mar 5, 2013
  3. */  
  4. package org.zlex.redis.support;  
  5.   
  6. import java.util.List;  
  7.   
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.data.redis.core.StringRedisTemplate;  
  10. import org.springframework.stereotype.Component;  
  11.   
  12. /**
  13. *  
  14. * @author snowolf
  15. * @version 1.0
  16. * @since 1.0
  17. */  
  18. @Component("listOps")  
  19. public class ListOps {  
  20.   
  21.     @Autowired  
  22.     private StringRedisTemplate stringRedisTemplate;  
  23.   
  24.     /**
  25.      * 压栈
  26.      *  
  27.      * @param key
  28.      * @param value
  29.      * @return
  30.      */  
  31.     public Long push(String key, String value) {  
  32.         return stringRedisTemplate.opsForList().leftPush(key, value);  
  33.     }  
  34.   
  35.     /**
  36.      * 出栈
  37.      *  
  38.      * @param key
  39.      * @return
  40.      */  
  41.     public String pop(String key) {  
  42.         return stringRedisTemplate.opsForList().leftPop(key);  
  43.     }  
  44.   
  45.     /**
  46.      * 入队
  47.      *  
  48.      * @param key
  49.      * @param value
  50.      * @return
  51.      */  
  52.     public Long in(String key, String value) {  
  53.         return stringRedisTemplate.opsForList().rightPush(key, value);  
  54.     }  
  55.   
  56.     /**
  57.      * 出队
  58.      *  
  59.      * @param key
  60.      * @return
  61.      */  
  62.     public String out(String key) {  
  63.         return stringRedisTemplate.opsForList().leftPop(key);  
  64.     }  
  65.   
  66.     /**
  67.      * 栈/队列长
  68.      *  
  69.      * @param key
  70.      * @return
  71.      */  
  72.     public Long length(String key) {  
  73.         return stringRedisTemplate.opsForList().size(key);  
  74.     }  
  75.   
  76.     /**
  77.      * 范围检索
  78.      *  
  79.      * @param key
  80.      * @param start
  81.      * @param end
  82.      * @return
  83.      */  
  84.     public List<String> range(String key, int start, int end) {  
  85.         return stringRedisTemplate.opsForList().range(key, start, end);  
  86.     }  
  87.   
  88.     /**
  89.      * 移除
  90.      *  
  91.      * @param key
  92.      * @param i
  93.      * @param value
  94.      */  
  95.     public void remove(String key, long i, String value) {  
  96.         stringRedisTemplate.opsForList().remove(key, i, value);  
  97.     }  
  98.   
  99.     /**
  100.      * 检索
  101.      *  
  102.      * @param key
  103.      * @param index
  104.      * @return
  105.      */  
  106.     public String index(String key, long index) {  
  107.         return stringRedisTemplate.opsForList().index(key, index);  
  108.     }  
  109.   
  110.     /**
  111.      * 置值
  112.      *  
  113.      * @param key
  114.      * @param index
  115.      * @param value
  116.      */  
  117.     public void set(String key, long index, String value) {  
  118.         stringRedisTemplate.opsForList().set(key, index, value);  
  119.     }  
  120.   
  121.     /**
  122.      * 裁剪
  123.      *  
  124.      * @param key
  125.      * @param start
  126.      * @param end
  127.      */  
  128.     public void trim(String key, long start, int end) {  
  129.         stringRedisTemplate.opsForList().trim(key, start, end);  
  130.     }  
  131. }  
复制代码


  1. /**
  2. * Mar 5, 2013
  3. */  
  4. package org.zlex.redis.support;  
  5.   
  6. import java.util.List;  
  7.   
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.data.redis.core.StringRedisTemplate;  
  10. import org.springframework.stereotype.Component;  
  11.   
  12. /**
  13. *  
  14. * @author snowolf
  15. * @version 1.0
  16. * @since 1.0
  17. */  
  18. @Component("listOps")  
  19. public class ListOps {  
  20.   
  21.     @Autowired  
  22.     private StringRedisTemplate stringRedisTemplate;  
  23.   
  24.     /**
  25.      * 压栈
  26.      *  
  27.      * @param key
  28.      * @param value
  29.      * @return
  30.      */  
  31.     public Long push(String key, String value) {  
  32.         return stringRedisTemplate.opsForList().leftPush(key, value);  
  33.     }  
  34.   
  35.     /**
  36.      * 出栈
  37.      *  
  38.      * @param key
  39.      * @return
  40.      */  
  41.     public String pop(String key) {  
  42.         return stringRedisTemplate.opsForList().leftPop(key);  
  43.     }  
  44.   
  45.     /**
  46.      * 入队
  47.      *  
  48.      * @param key
  49.      * @param value
  50.      * @return
  51.      */  
  52.     public Long in(String key, String value) {  
  53.         return stringRedisTemplate.opsForList().rightPush(key, value);  
  54.     }  
  55.   
  56.     /**
  57.      * 出队
  58.      *  
  59.      * @param key
  60.      * @return
  61.      */  
  62.     public String out(String key) {  
  63.         return stringRedisTemplate.opsForList().leftPop(key);  
  64.     }  
  65.   
  66.     /**
  67.      * 栈/队列长
  68.      *  
  69.      * @param key
  70.      * @return
  71.      */  
  72.     public Long length(String key) {  
  73.         return stringRedisTemplate.opsForList().size(key);  
  74.     }  
  75.   
  76.     /**
  77.      * 范围检索
  78.      *  
  79.      * @param key
  80.      * @param start
  81.      * @param end
  82.      * @return
  83.      */  
  84.     public List<String> range(String key, int start, int end) {  
  85.         return stringRedisTemplate.opsForList().range(key, start, end);  
  86.     }  
  87.   
  88.     /**
  89.      * 移除
  90.      *  
  91.      * @param key
  92.      * @param i
  93.      * @param value
  94.      */  
  95.     public void remove(String key, long i, String value) {  
  96.         stringRedisTemplate.opsForList().remove(key, i, value);  
  97.     }  
  98.   
  99.     /**
  100.      * 检索
  101.      *  
  102.      * @param key
  103.      * @param index
  104.      * @return
  105.      */  
  106.     public String index(String key, long index) {  
  107.         return stringRedisTemplate.opsForList().index(key, index);  
  108.     }  
  109.   
  110.     /**
  111.      * 置值
  112.      *  
  113.      * @param key
  114.      * @param index
  115.      * @param value
  116.      */  
  117.     public void set(String key, long index, String value) {  
  118.         stringRedisTemplate.opsForList().set(key, index, value);  
  119.     }  
  120.   
  121.     /**
  122.      * 裁剪
  123.      *  
  124.      * @param key
  125.      * @param start
  126.      * @param end
  127.      */  
  128.     public void trim(String key, long start, int end) {  
  129.         stringRedisTemplate.opsForList().trim(key, start, end);  
  130.     }  
  131. }  
复制代码







这里说明下,例如LPUSH,RPUSH,其实就是从左边压栈,还是从右边压栈的不同命令。可以把堆栈看作是一个从左至右的数组。如果左边压栈,右边出栈,那就是队列的入队/出队操作;如果左边压栈,左边出栈,那就是堆栈操作。

举个具体的例子:
队列操作:LPUSH入队,RPOP出队,同理,可把L|R替换。
堆栈操作:LPUSH压栈,LPOP出栈,同理,可把L|R替换。

下面进行测试用例,初始、结束时,分别做入队、出队操作,期间进行堆栈,队列操作。不用我细说了,看测试用例,很简单!
  1. /**
  2. * Mar 5, 2013
  3. */  
  4. package org.zlex.redis;  
  5.   
  6. import static org.junit.Assert.*;  
  7.   
  8. import java.util.List;  
  9.   
  10. import org.junit.Before;  
  11. import org.junit.After;  
  12. import org.junit.Test;  
  13. import org.springframework.context.ApplicationContext;  
  14. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  15. import org.zlex.redis.support.ListOps;  
  16.   
  17. /**
  18. *  
  19. * @author snowolf
  20. * @version 1.0
  21. * @since 1.0
  22. */  
  23. public class ListOpsTest {  
  24.     private ApplicationContext app;  
  25.     private ListOps listOps;  
  26.     private String key = "queue";  
  27.   
  28.     @Before  
  29.     public void before() throws Exception {  
  30.         app = new ClassPathXmlApplicationContext("applicationContext.xml");  
  31.         listOps = (ListOps) app.getBean("listOps");  
  32.   
  33.         System.out.println("------------IN---------------");  
  34.         for (int i = 0; i < 5; i++) {  
  35.             String uid = "u" + i;  
  36.             System.out.println(uid);  
  37.             listOps.in(key, uid);  
  38.         }  
  39.     }  
  40.   
  41.     @After  
  42.     public void after() {  
  43.         // ------------OUT---------------  
  44.         System.out.println("------------OUT---------------");  
  45.         long length = listOps.length(key);  
  46.         for (long i = 0; i < length; i++) {  
  47.             String uid = listOps.out(key);  
  48.             System.out.println(uid);  
  49.         }  
  50.     }  
  51.   
  52.     @Test  
  53.     public void stack() {  
  54.         // ------------PUSH---------------  
  55.         String key = "stack";  
  56.         int len = 5;  
  57.         System.out.println("------------PUSH---------------");  
  58.         for (int i = 0; i < len; i++) {  
  59.             String uid = "u" + System.currentTimeMillis();  
  60.             System.out.println(uid);  
  61.             listOps.push(key, uid);  
  62.         }  
  63.   
  64.         long length = listOps.length(key);  
  65.         assertEquals(len, length);  
  66.   
  67.         // ------------POP---------------  
  68.         System.out.println("------------POP---------------");  
  69.         for (long i = 0; i < length; i++) {  
  70.             String uid = listOps.pop(key);  
  71.             System.out.println(uid);  
  72.         }  
  73.     }  
  74.   
  75.     @Test  
  76.     public void index() {  
  77.   
  78.         // -------------INDEX-------------  
  79.         String value = listOps.index(key, 3);  
  80.         assertEquals("u3", value);  
  81.     }  
  82.   
  83.     @Test  
  84.     public void range() {  
  85.         // -------------RANGE-------------  
  86.         List<String> list = listOps.range(key, 3, 5);  
  87.         boolean result1 = list.contains("u3");  
  88.         assertEquals(true, result1);  
  89.   
  90.         boolean result2 = list.contains("u1");  
  91.         assertEquals(false, result2);  
  92.     }  
  93.   
  94.     @Test  
  95.     public void trim() {  
  96.         // ------------TRIM---------------  
  97.         List<String> list = listOps.range(key, 3, 5);  
  98.         listOps.trim(key, 3, 5);  
  99.         boolean result3 = list.contains("u1");  
  100.         assertEquals(false, result3);  
  101.     }  
  102.   
  103.     @Test  
  104.     public void set() {  
  105.         // ------------SET-----------------  
  106.         List<String> list = listOps.range(key, 3, 5);  
  107.         listOps.set(key, 4, "ux4");  
  108.         boolean result4 = list.contains("u4");  
  109.         assertEquals(true, result4);  
  110.   
  111.     }  
  112.   
  113.     @Test  
  114.     public void remove() {  
  115.         // ------------REMOVE-----------------  
  116.         listOps.remove(key, 4, "u4");  
  117.         String value = listOps.index(key, 4);  
  118.         assertEquals(null, value);  
  119.   
  120.     }  
  121. }  
复制代码


  1. /**
  2. * Mar 5, 2013
  3. */  
  4. package org.zlex.redis;  
  5.   
  6. import static org.junit.Assert.*;  
  7.   
  8. import java.util.List;  
  9.   
  10. import org.junit.Before;  
  11. import org.junit.After;  
  12. import org.junit.Test;  
  13. import org.springframework.context.ApplicationContext;  
  14. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  15. import org.zlex.redis.support.ListOps;  
  16.   
  17. /**
  18. *  
  19. * @author snowolf
  20. * @version 1.0
  21. * @since 1.0
  22. */  
  23. public class ListOpsTest {  
  24.     private ApplicationContext app;  
  25.     private ListOps listOps;  
  26.     private String key = "queue";  
  27.   
  28.     @Before  
  29.     public void before() throws Exception {  
  30.         app = new ClassPathXmlApplicationContext("applicationContext.xml");  
  31.         listOps = (ListOps) app.getBean("listOps");  
  32.   
  33.         System.out.println("------------IN---------------");  
  34.         for (int i = 0; i < 5; i++) {  
  35.             String uid = "u" + i;  
  36.             System.out.println(uid);  
  37.             listOps.in(key, uid);  
  38.         }  
  39.     }  
  40.   
  41.     @After  
  42.     public void after() {  
  43.         // ------------OUT---------------  
  44.         System.out.println("------------OUT---------------");  
  45.         long length = listOps.length(key);  
  46.         for (long i = 0; i < length; i++) {  
  47.             String uid = listOps.out(key);  
  48.             System.out.println(uid);  
  49.         }  
  50.     }  
  51.   
  52.     @Test  
  53.     public void stack() {  
  54.         // ------------PUSH---------------  
  55.         String key = "stack";  
  56.         int len = 5;  
  57.         System.out.println("------------PUSH---------------");  
  58.         for (int i = 0; i < len; i++) {  
  59.             String uid = "u" + System.currentTimeMillis();  
  60.             System.out.println(uid);  
  61.             listOps.push(key, uid);  
  62.         }  
  63.   
  64.         long length = listOps.length(key);  
  65.         assertEquals(len, length);  
  66.   
  67.         // ------------POP---------------  
  68.         System.out.println("------------POP---------------");  
  69.         for (long i = 0; i < length; i++) {  
  70.             String uid = listOps.pop(key);  
  71.             System.out.println(uid);  
  72.         }  
  73.     }  
  74.   
  75.     @Test  
  76.     public void index() {  
  77.   
  78.         // -------------INDEX-------------  
  79.         String value = listOps.index(key, 3);  
  80.         assertEquals("u3", value);  
  81.     }  
  82.   
  83.     @Test  
  84.     public void range() {  
  85.         // -------------RANGE-------------  
  86.         List<String> list = listOps.range(key, 3, 5);  
  87.         boolean result1 = list.contains("u3");  
  88.         assertEquals(true, result1);  
  89.   
  90.         boolean result2 = list.contains("u1");  
  91.         assertEquals(false, result2);  
  92.     }  
  93.   
  94.     @Test  
  95.     public void trim() {  
  96.         // ------------TRIM---------------  
  97.         List<String> list = listOps.range(key, 3, 5);  
  98.         listOps.trim(key, 3, 5);  
  99.         boolean result3 = list.contains("u1");  
  100.         assertEquals(false, result3);  
  101.     }  
  102.   
  103.     @Test  
  104.     public void set() {  
  105.         // ------------SET-----------------  
  106.         List<String> list = listOps.range(key, 3, 5);  
  107.         listOps.set(key, 4, "ux4");  
  108.         boolean result4 = list.contains("u4");  
  109.         assertEquals(true, result4);  
  110.   
  111.     }  
  112.   
  113.     @Test  
  114.     public void remove() {  
  115.         // ------------REMOVE-----------------  
  116.         listOps.remove(key, 4, "u4");  
  117.         String value = listOps.index(key, 4);  
  118.         assertEquals(null, value);  
  119.   
  120.     }  
  121. }  
复制代码




回头继续整理,这个套路没错!

回复

使用道具 举报

fangzhou200008 发表于 2016-4-8 17:53:01
感谢楼主分享。
回复

使用道具 举报

Pengjx2015 发表于 2016-4-8 21:06:25
感谢楼主分享。
回复

使用道具 举报

惹尘埃53 发表于 2018-1-9 11:04:23
感谢楼主分享。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条