List数据存放至Redis,刷新缓存

1.初学习Redis,记录一下与List相关的一些操作

  
  
  
  
  1. public void pushRedis(List<Store> stores){//刷新缓存
  2. StringBuffer buffer = new StringBuffer(); for(int i = 0; i < stores.size();i++){ Store store = stores.get(i); store.setIndexid(i); if(null == store.getId() || 0 == store.getId()){ storeDAO.insert(store); }else{ storeDAO.updateByPrimaryKey(store); } buffer.append(store.getItemid()).append(","); } redis.save("itemId", buffer.toString()); }
  3.  
  4. public List<Object> getItemList() { 
  5.     return redis.lRange("store-list"0, -1); 
  6. public List<Store> update(List<Store> stores,int nowIndex,int reIndex){ Store store = stores.get(nowIndex); stores.remove(nowIndex); stores.add(reIndex - 1,store);
  7. return stores; }//根据List中索引值改变元素位置

2. 备注:在applicationContext.xml进行相关的配置

  
  
  
  
  1. <property name="locations"> 
  2.     <list> 
  3.         <value>/WEB-INF/redis.properties</value> 
  4.         <value>/WEB-INF/jdbc.properties</value> 
  5.     </list> 
  6. </property> 
  7. <import resource="redis-config.xml" />

3.附带一个简单Redis测试类

  
  
  
  
  1. public class Redis { 
  2.  
  3.     public static void main(String[] args) { 
  4.  
  5.         Jedis jedis = new Jedis("192.168.1.103",4352); 
  6.          
  7.         //简单的key-value 存储 
  8.         jedis.set("tn""thisuc"); 
  9.          
  10.         System.out.println(jedis.get("tn")); 
  11.          
  12.         //在原有值的基础上添加,如若之前没有该key,则导入该key 
  13.         //之前已经设定了redis对应"myredis",此可执行便会使redis对应"myredisyourredis" 
  14.         jedis.append("redis","yourredis"); 
  15.         jedis.append("content""rabbit"); 
  16.          
  17.         System.out.println(jedis.get("redis")); 
  18.          
  19.         //mset 是设置多个key value值 参数(key1,value1,key2,value2,key3,value3...) 
  20.         //mget 是获取多个key所对应的value值 参数(key1,key2,key3,...,keyn)  
  21.             jedis.mset("name1","thisuc","name2","gaoxc","name3","lemon"); 
  22.         System.out.println(jedis.mget("name1","name2","name3")); 
  23.     } 

 

你可能感兴趣的:(redis,list索引值改变位置)