接口:
package com.net.test.redis.base.dao; import java.util.List; /** * @author *** * @Time:2017年8月10日 上午9:23:07 * @version 1.0 * @description */ public interface IRedisDaoList { public void lPush(String key, String value); public void lPushAll(String key, List<String> list); public void set(String key, int offset, String value); public void insert(String key, String value1, String value2); public void getValueByIndex(String key, int offset); public void size(String key); public void remove(String key, long count, String value); public void trim(String key, int start, int end); public void lpop(String key); public void lrange(String key, int start, int end); public void rightPopAndLeftPush(String sourceKey, String destinationKey); }
实现类:
package com.net.test.redis.base.dao.imp; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.ListOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Repository; import com.net.test.redis.base.dao.IRedisDaoList; /** * @author *** * @Time:2017年8月10日 上午10:58:42 * @version 1.0 * @description */ @Repository public class RedisDaoListImp implements IRedisDaoList { @Autowired private RedisTemplate<String, String> redis; /** * @description 简单的往数组里面添加元素 * @param key * @param value */ @Override public void lPush(String key, String value) { ListOperations<String, String> oper = redis.opsForList(); int backValue = oper.leftPush(key, value).intValue(); System.out.println("返回值 : " + backValue); } /** * @description 批量往数组里面添加元素 * @param key * @param list */ public void lPushAll(String key, List<String> list) { ListOperations<String, String> oper = redis.opsForList(); int backValue = oper.leftPushAll(key, list).intValue(); System.out.println("返回值 : " + backValue); } /** * @description 对指定下标的数组元素进行替换 * @param key * @param offset * @param value */ @Override public void set(String key, int offset, String value) { ListOperations<String, String> oper = redis.opsForList(); oper.set(key, offset, value); } /** * @description 对指定下标的数组进行插入数据 * @param key * @param value1 原有值 * @param value2 插入的值 */ @Override public void insert(String key, String value1, String value2) { ListOperations<String, String> oper = redis.opsForList(); int backValue = oper.leftPush(key, value1, value2).intValue(); System.out.println("返回值 : " + backValue); } /** * @description 获取指定下标的数组元素 * @param key * @param offset */ @Override public void getValueByIndex(String key, int offset) { ListOperations<String, String> oper = redis.opsForList(); String value = oper.index(key, offset); System.out.println(offset + "位置下的值为 : " + value); } /** * @description 获取数组长度 * @param key */ @Override public void size(String key) { ListOperations<String, String> oper = redis.opsForList(); int length = oper.size(key).intValue(); System.out.println(key + " 数组长度为 : " + length); } /** * @description 移除数组匹配到的数据元素 * @param key * @param count 负数:从右往左 整数:从左往右 * @param value 移除的值 */ @Override public void remove(String key, long count, String value) { ListOperations<String, String> oper = redis.opsForList(); int backValue = oper.remove(key, count, value).intValue(); System.out.println(key + " 数组长度为 : " + backValue); } /** * @description 保留区间内的元素,区间外的全部删除 * @param key * @param start 区间开始 * @param end 区间结束 */ @Override public void trim(String key, int start, int end) { ListOperations<String, String> oper = redis.opsForList(); oper.trim(key, start, end); } /** * @description 从左到右,删除第一个元素 * @param key */ public void lpop(String key) { ListOperations<String, String> oper = redis.opsForList(); String value = oper.leftPop(key); System.err.println("移除的元素 : " + value); } /** * @description 查询区间范围内的元素 * @param key * @param start * @param end */ @Override public void lrange(String key, int start, int end) { ListOperations<String, String> oper = redis.opsForList(); List<String> list = oper.range(key, start, end); for(String str : list) { System.out.println("遍历 : " + str); } } /** * @description 两个list之间移元素 * @param sourceKey 源 * @param destinationKey 目的地 */ @Override public void rightPopAndLeftPush(String sourceKey, String destinationKey) { ListOperations<String, String> oper = redis.opsForList(); String v = oper.rightPopAndLeftPush(sourceKey, destinationKey); System.out.println("----------" + v); } }