1.初学习Redis,记录一下与List相关的一些操作
- public void pushRedis(List<Store> 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(","); } redis.save("itemId", buffer.toString()); }
- public List<Object> getItemList() {
- return redis.lRange("store-list", 0, -1);
- }
- public List<Store> update(List<Store> stores,int nowIndex,int reIndex){ Store store = stores.get(nowIndex); stores.remove(nowIndex); stores.add(reIndex - 1,store);
- return stores; }//根据List中索引值改变元素位置
2. 备注:在applicationContext.xml进行相关的配置
- <property name="locations">
- <list>
- <value>/WEB-INF/redis.properties</value>
- <value>/WEB-INF/jdbc.properties</value>
- </list>
- </property>
- <import resource="redis-config.xml" />
3.附带一个简单Redis测试类
- public class Redis {
- public static void main(String[] args) {
- Jedis jedis = new Jedis("192.168.1.103",4352);
- //简单的key-value 存储
- jedis.set("tn", "thisuc");
- System.out.println(jedis.get("tn"));
- //在原有值的基础上添加,如若之前没有该key,则导入该key
- //之前已经设定了redis对应"myredis",此可执行便会使redis对应"myredisyourredis"
- jedis.append("redis","yourredis");
- jedis.append("content", "rabbit");
- System.out.println(jedis.get("redis"));
- //mset 是设置多个key value值 参数(key1,value1,key2,value2,key3,value3...)
- //mget 是获取多个key所对应的value值 参数(key1,key2,key3,...,keyn)
- jedis.mset("name1","thisuc","name2","gaoxc","name3","lemon");
- System.out.println(jedis.mget("name1","name2","name3"));
- }
- }