当前位置:网站首页>Hash、Set、List、Zset、BitMap、Scan
Hash、Set、List、Zset、BitMap、Scan
2022-07-27 18:55:00 【Get lazy】
@SpringBootTest
class HashDemo {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private final String key = "class#1";
@Test
public void test() {
stringRedisTemplate.opsForHash().put(key, "id", "12");
stringRedisTemplate.opsForHash().put(key, "name", "12");
stringRedisTemplate.opsForHash().put(key, "age", "12");
stringRedisTemplate.opsForHash().put(key, "address", "12");
Object name = stringRedisTemplate.opsForHash().get(key, "name");
System.out.println(name);
Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries(key);
System.out.println(entries);
List<Object> hashsKey = new ArrayList<>();
hashsKey.add("id");
hashsKey.add("name");
List<Object> objects = stringRedisTemplate.opsForHash().multiGet(key, hashsKey);
System.out.println(objects);
}
}
@SpringBootTest
class ListDemo {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private final String key1 = "class#206";
@Test
public void test() {
stringRedisTemplate.opsForList().leftPush(key1,"zhangsan");
stringRedisTemplate.opsForList().leftPush(key1,"lisi");
stringRedisTemplate.opsForList().leftPush(key1,"wangwu");
stringRedisTemplate.opsForList().set(key1,1,"11");
String password = stringRedisTemplate.opsForList().leftPop(key1);
System.out.println(password);
}
}
@SpringBootTest
class SetDemo {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private final String key2 = "class#207";
private final String key3 = "class#208";
@Test
public void test() {
stringRedisTemplate.opsForSet().add(key2, "s");
stringRedisTemplate.opsForSet().add(key2, "a");
stringRedisTemplate.opsForSet().add(key3, "a");
stringRedisTemplate.opsForSet().add(key3, "d");
// Combine
Set<String> union = stringRedisTemplate.opsForSet().union(key2, key3);
System.out.println(union);
// Difference set
Set<String> difference = stringRedisTemplate.opsForSet().difference(key2, key3);
System.out.println(difference);
// intersection
Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key2, key3);
System.out.println(intersect);
}
}
@SpringBootTest
class ZsetDemo {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private final String kk = "class#209";
private final String key5 = "class#210";
@Test
public void test() {
stringRedisTemplate.opsForZSet().add(kk, " Chinese language and literature ", 99);
stringRedisTemplate.opsForZSet().add(kk, " English ", 80);
stringRedisTemplate.opsForZSet().add(kk, " mathematics ", 150);
stringRedisTemplate.opsForZSet().add(kk, " Politics ", 66);
System.out.println(" Find the maximum ");
ZSetOperations.TypedTuple<String> stringTypedTuple = stringRedisTemplate.opsForZSet().popMax(kk);
System.out.println(stringTypedTuple);
System.out.println(" Find the minimum ");
ZSetOperations.TypedTuple<String> stringTypedTuple1 = stringRedisTemplate.opsForZSet().popMin(kk);
System.out.println(stringTypedTuple1);
System.out.println(" Find the second ");
Set<ZSetOperations.TypedTuple<String>> typedTuples = stringRedisTemplate.opsForZSet().popMin(kk, 2);
System.out.println(typedTuples);
}
}
@SpringBootTest
public class BitMap {
@Resource (name="redisTemplate")
private ValueOperations valueOperations;
@Autowired
private StringRedisTemplate stringRedisTemplate;
private final Integer user1=1;
private final Integer user2=2;
private final Integer user3=3;
private final String key20220601="20220601";
private final String key20220602="20220602";
private final String key20220603="20220603";
private final String deskey="20220603-20220602";
@Test
public void test(){
valueOperations.setBit(key20220601,user1,true);
valueOperations.setBit(key20220601,user2,true);
valueOperations.setBit(key20220602,user2,true);
valueOperations.setBit(key20220603,user2,true);
valueOperations.setBit(key20220603,user3,true);
RedisCallback<Long> redisCallback=new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.bitOp(RedisStringCommands.BitOperation.AND,deskey.getBytes(),key20220601.getBytes(), key20220602.getBytes(), key20220603.getBytes());
}
};
Long execute = stringRedisTemplate.execute(redisCallback);
System.out.println(execute);
}
}
@SpringBootTest
public class ScanTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private final String key111="class#111";
@Test
public void test(){
for (int i=0;i<100;i++){
stringRedisTemplate.opsForValue().set("a"+i, "value"+i);
}
for (int i=0;i<100;i++){
stringRedisTemplate.opsForValue().set("b"+i, "value"+i);
}
ScanOptions scanOps = ScanOptions.scanOptions().match("b*").count(2).build();
Cursor<String> cursor = stringRedisTemplate.scan(scanOps);
while (cursor.hasNext()){
System.out.println(cursor.next());
}
// Close cursor
cursor.close();
}
}
边栏推荐
- Visual studio code installation tutorial (super detailed)
- Talking about JVM (frequent interview)
- 文件的上传和下载
- 面试官:你觉得你最大的缺点是什么?
- Idea 2020.1 Community Edition download experience
- 家用静音驱蚊灯芯片-DLTAP703SD-杰力科创
- MySQL 02 初体验
- Generate PDM file from Navicat export table
- JS to achieve smooth scrolling of pages or DOM elements
- js实现简易表单验证与全选功能
猜你喜欢

Examples of map search

Overview of Baidu map technology, and application development of basic API and webapi

LED学习护眼台灯触摸芯片-DLT8T10S-杰力科创

文件的上传和下载

Music rhythm colorful gradient lamp chip -- dlt8s04a- Jericho

Order timeout cancellation and commodity query by category

Redis注解

个人中心--订单业务流程
MySQL create event execution task

USB充电式暖手宝芯片-DLTAP602SC-杰力科创
随机推荐
Valueerror: found input variables with inconsistent numbers of samples: [80019456, 26673152] [error reporting]
Build a simple knowledge question and answer system
Interceptor拦截器
EN 1155 building hardware swing door opener - CE certification
Examples of map search
文件的上传和下载
MySQL 05 存储过程
js中的函数与DOM获取元素和事件属性的使用
迷你洗衣机触摸芯片-DLT8MA12TS-杰力科创
Infrared hyperspectral survey
Uniapp H5 cross domain problem
10 SQL optimization schemes summarized by Alibaba P8 (very practical)
Led learning eye protection table lamp touch chip-dlt8t10s-jericho
The song of the virtual idol was originally generated in this way!
"MySQL things" explains the indexing principle in detail
Matplotlib(基本用法)
filebeat.yml配置文件关于多个服务的配置问题
Log4j epic loopholes, big companies like jd.com have been recruited
MySQL 03 advanced query (I)
TypeScript安装