micromax 发表于 2018-11-7 11:24:20

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

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


[*]public void pushRedis(List stores){//刷新缓存
[*]    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(&quot;,&quot;);      } redis.save(&quot;itemId&quot;, buffer.toString()); }
[*]
[*]public List getItemList() {
[*]    return redis.lRange(&quot;store-list&quot;, 0, -1);
[*]}
[*]public List update(List stores,int nowIndex,int reIndex){ Store store = stores.get(nowIndex); stores.remove(nowIndex); stores.add(reIndex - 1,store);
[*]return stores; }//根据List中索引值改变元素位置
  

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


[*]
[*]   
[*]      /WEB-INF/redis.properties
[*]      /WEB-INF/jdbc.properties
[*]   
[*]
[*]
  

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


[*]public class Redis {
[*]
[*]    public static void main(String[] args) {
[*]
[*]      Jedis jedis = new Jedis(&quot;192.168.1.103&quot;,4352);
[*]
[*]      //简单的key-value 存储
[*]      jedis.set(&quot;tn&quot;, &quot;thisuc&quot;);
[*]
[*]      System.out.println(jedis.get(&quot;tn&quot;));
[*]
[*]      //在原有值的基础上添加,如若之前没有该key,则导入该key
[*]      //之前已经设定了redis对应&quot;myredis&quot;,此可执行便会使redis对应&quot;myredisyourredis&quot;
[*]      jedis.append(&quot;redis&quot;,&quot;yourredis&quot;);
[*]      jedis.append(&quot;content&quot;, &quot;rabbit&quot;);
[*]
[*]      System.out.println(jedis.get(&quot;redis&quot;));
[*]
[*]      //mset 是设置多个key value值 参数(key1,value1,key2,value2,key3,value3...)
[*]      //mget 是获取多个key所对应的value值 参数(key1,key2,key3,...,keyn)
[*]            jedis.mset(&quot;name1&quot;,&quot;thisuc&quot;,&quot;name2&quot;,&quot;gaoxc&quot;,&quot;name3&quot;,&quot;lemon&quot;);
[*]      System.out.println(jedis.mget(&quot;name1&quot;,&quot;name2&quot;,&quot;name3&quot;));
[*]    }
[*]}
  



页: [1]
查看完整版本: List数据存放至Redis,刷新缓存