|
String類型:ValueOperations @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-redis.xml")
public class RedisStringTest {
@Autowired
private RedisTemplate redisTemplate;
private ValueOperations<String, String> ops = null;
@Before
public void init() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
ops = redisTemplate.opsForValue();
}
@Test
public void testSet() {
ops.set("name", "zhangsan");
ops.set("sex", "男", 10, TimeUnit.SECONDS); // 有效時間10秒
ops.set("name", "xx", 2); // 替換,索引從0開始 zhxxgsan
// 當(dāng)key不存在的時候,執(zhí)行保存操作;當(dāng)key存在的時候,什么都不做
ops.setIfAbsent("name", "lily");
// 批量保存
Map map = new HashMap();
map.put("name2", "fan2");
map.put("name3", "fan3");
map.put("name4", "fan4");
ops.multiSet(map);
// 追加 當(dāng)key存在時,會執(zhí)行追加操作;當(dāng)key不存在時,會執(zhí)行保存操作
ops.append("name5", "fan5"); // 執(zhí)行兩次,結(jié)果:fan5fan5
}
@Test
public void testGet() {
// 根據(jù)key獲取value
String value = ops.get("name");
System.out.println(value);
// 首先根據(jù)key獲取value,然后再根據(jù)value進行截取,從start位置截取到end位置[包含start和end],索引從0開始
String name = ops.get("name", 2, 5);
System.out.println(name);
// 批量獲取
List<String> keys = new ArrayList<>();
keys.add("name2");
keys.add("name3");
keys.add("name4");
List<String> values = ops.multiGet(keys);
for (String s : values) {
System.out.println(s);
}
// 根據(jù)key獲取value的長度
Long size = ops.size("name");
System.out.println(size);
}
@Test
public void testIncrement() {
ops.set("age", "20");
ops.increment("age"); // 自增1
System.out.println(ops.get("age")); // 21
ops.increment("age", 5); // 自增5
System.out.println(ops.get("age"));// 26
ops.decrement("age"); // 自減
System.out.println(ops.get("age")); // 25
}
/**
* 注意:刪除使用的是RedisTemplate(redisTemplate),而不是ValueOperations(ops)
*/
@Test
public void testDelete() {
// 單個刪除
redisTemplate.delete("name2");
// 批量刪除
List<String> keys = new ArrayList<>();
keys.add("name3");
keys.add("name4");
keys.add("name5");
redisTemplate.delete(keys);
}
}
Hash類型:HashOperations /**
* 注意:實體類必須實現(xiàn)序列化接口
*/
public class Article implements Serializable {
//作者
private String author;
//創(chuàng)建時間
private Date createTime;
//標(biāo)題
private String title;
/************* get/set方法 *************/
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-redis.xml")
public class RedisHashTest {
@Autowired
private RedisTemplate redisTemplate;
private HashOperations<String, String, Article> ops = null;
@Before
public void init() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
ops = redisTemplate.opsForHash();
}
/**
* 保存
*/
@Test
public void testPut() {
Article article = new Article();
article.setAuthor("貓膩");
article.setTitle("將夜");
article.setCreateTime(new Date());
ops.put("article", "1", article);
// ops.putAll(Map);
// ops.putIfAbsent();
}
/**
* 獲取
*/
@Test
public void testGet() {
// 判斷hashkey是否存在
Boolean flag = ops.hasKey("article", "2");
System.out.println(flag);
// 根據(jù)key和hashkey獲取值
Article article = ops.get("article", "1");
System.out.println(article);
// 根據(jù)key獲取所有的hashkey
Set<String> set = ops.keys("article");
for (String s : set) {
System.out.println(s);
}
// 根據(jù)key獲取所有的值
List<Article> list = ops.values("article");
for (Article a : list) {
System.out.println(a);
}
// 同時獲取hashkey和value
Map<String, Article> map = ops.entries("article");
Set<Map.Entry<String, Article>> entries = map.entrySet();
for (Map.Entry<String, Article> entry : entries) {
System.out.println(entry.getKey() "---" entry.getValue());
}
}
/**
* 刪除
*/
@Test
public void testDelete() {
// 這里通過刪除hashkey,來刪除對應(yīng)的值
// 如果要把整個hash刪除,參考String類型的 redisTemplate.delete(key);
ops.delete("article", "1");
// ops.delete("article", "1", "2", ...);
}
}
List類型:ListOperations @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-redis.xml")
public class RedisListTest {
@Autowired
private RedisTemplate redisTemplate;
private ListOperations<String, String> ops = null;
@Before
public void init() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
ops = redisTemplate.opsForList();
}
/**
* 添加
*/
@Test
public void testAdd() {
// 從左邊添加
ops.leftPush("student", "lily");
ops.leftPushAll("student", "小明", "小紅", "小芳");
ArrayList<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
ops.leftPushAll("student", list);
// 從右邊添加
ops.rightPush("student", "ddd");
// ops.rightPushAll();
}
/**
* 查詢
*/
@Test
public void testGet() {
String student = ops.index("student", 1); // 索引從0開始
String student1 = ops.index("student", -1); // -1代表右邊開始第一個元素
String student2 = ops.index("student", -2); // -2代表右邊開始第二個元素
System.out.println(student);
System.out.println(student1);
System.out.println(student2);
// 范圍查詢,包括開始索引和結(jié)束索引
List<String> list = ops.range("student", 0, 3);
System.out.println(list);
}
/**
* 刪除
*/
@Test
public void testRemove() {
String student = ops.leftPop("student");
System.out.println(student);
String student1 = ops.rightPop("student");
System.out.println(student1);
// 彈出指定的元素
// count > 0:刪除左邊起第幾個等于指定值的元素
// count < 0:刪除右邊起第幾個等于指定值的元素
// count = 0:刪除所有等于value的元素
ops.remove("student", 2, "小紅");
}
}
Set類型:SetOperations @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-redis.xml")
public class RedisSetTest {
@Autowired
private RedisTemplate redisTemplate;
private SetOperations<String, String> ops = null;
@Before
public void init() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
ops = redisTemplate.opsForSet();
}
@Test
public void testAdd() {
ops.add("person", "張三", "李四", "王五", "趙六");
ops.add("person", "張三");
}
/**
* 查詢
*/
@Test
public void testFind() {
Set<String> person = ops.members("person");
for (String s : person) {
System.out.println(s);
}
// 隨機獲取一個
String s = ops.randomMember("person");
System.out.println(s);
// 隨機獲取多個(可能會重復(fù))
List<String> list = ops.randomMembers("person", 3);
for (String name : list) {
System.out.println(name);
}
}
/**
* 刪除
*/
@Test
public void testRemove() {
// 返回移除成功的個數(shù)
Long count = ops.remove("person", "張三", "李四", "趙六");
System.out.println(count);
// 隨機移除指定個數(shù)元素
List<String> person = ops.pop("person", 2);
System.out.println(person);
String p = ops.pop("person");// 隨機移除一個
System.out.println(p);
}
/**
* 多集合操作
*/
@Test
public void testMoreSet() {
ops.add("names1", "zhangsan", "lisi", "wangwu");
ops.add("names2", "zhangsan", "lisi", "zhaoliu");
// 求交集
Set<String> set1 = ops.intersect("names1", "names2");
System.out.println(set1);
// 求并集
Set<String> set2 = ops.union("names1", "names2");
System.out.println(set2);
// 求差集
Set<String> set3 = ops.difference("names1", "names2");
System.out.println(set3); // wangwu
Set<String> set4 = ops.difference("names2", "names1");
System.out.println(set4); // zhaoliu
}
}
ZSet類型:ZSetOperations @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-redis.xml")
public class RedisZSetTest {
@Autowired
private RedisTemplate redisTemplate;
private ZSetOperations<String, String> ops = null;
@Before
public void init() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
ops = redisTemplate.opsForZSet();
}
/**
* 添加
*/
@Test
public void testAdd() {
ops.add("student", "aaa", 40);
ops.add("student", "bbb", 80);
ops.add("student", "ccc", 60);
}
/**
* 分數(shù)的增減
*/
@Test
public void testScore() {
ops.incrementScore("student", "aaa", 60); // 增加60分
ops.incrementScore("student", "bbb", -50); // 減少50分
}
/**
* 查詢一個元素
*/
@Test
public void testFindOne() {
// 查詢個人分數(shù)
Double score = ops.score("student", "aaa");
System.out.println(score);
// 查詢一個元素在集合中的排名 從0開始
Long rank = ops.rank("student", "bbb");
System.out.println(rank);
}
/**
* 根據(jù)一個區(qū)間獲取一個列表
*/
@Test
public void testFindList() {
// 根據(jù)排名區(qū)間獲取列表
Set<String> set1 = ops.range("student", 1, 3);
System.out.println(set1);
Set<ZSetOperations.TypedTuple<String>> set2 = ops.rangeWithScores("student", 1, 3);
for (ZSetOperations.TypedTuple<String> tuple : set2) {
System.out.println(tuple.getValue() "---" tuple.getScore());
}
// 根據(jù)分數(shù)區(qū)間獲取列表
Set<String> set3 = ops.rangeByScore("student", 60, 90);
System.out.println(set3);
Set<ZSetOperations.TypedTuple<String>> set4 = ops.rangeByScoreWithScores("student", 60, 90);
for (ZSetOperations.TypedTuple<String> tuple : set4) {
System.out.println(tuple.getValue() "--" tuple.getScore());
}
}
/**
* 統(tǒng)計
*/
@Test
public void testCount() {
// 統(tǒng)計一個集合中的元素
Long zCard = ops.zCard("student");
System.out.println(zCard);
Long count = ops.count("student", 60, 90);
// 根據(jù)一個分數(shù)區(qū)間統(tǒng)計元素數(shù)量
System.out.println(count);
}
/**
* 刪除
*/
@Test
public void testRemove() {
// 通過key--value刪除 value允許傳入多個
Long remove = ops.remove("student", "aaa", "bbb");
System.out.println(remove); // 返回成功刪除的個數(shù)
// 通過排名區(qū)間刪除
Long count1 = ops.removeRange("student", 1, 2);
System.out.println(count1);
// 通過分數(shù)區(qū)間刪除
Long count2 = ops.removeRangeByScore("student", 30, 60);
System.out.println(count2);
}
}
? 來源:https://www./content-2-656401.html |
|
|