HashOperations提供一系列方法操作hash。首先初始化spring工厂获得redisTemplate和opsForHash

    private RedisTemplate<String,Object> redisTemplate;
private HashOperations<String,String,Object> opsForHash; @SuppressWarnings("unchecked")
@Before
public void before(){
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
redisTemplate = (RedisTemplate<String,Object>)context.getBean("redisTemplate");
opsForHash = redisTemplate.opsForHash();
}
void put(H key, HK hashKey, HV value);
Map< HK, HV > entries(H key);
    @Test
public void testPut(){
opsForHash.put("he1", "key1", "a");
opsForHash.put("he1", "key2", "b");
opsForHash.put("he1", "key3", "c");
Map<String, Object> entries = opsForHash.entries("he1");
System.out.println(entries);//{key3=c, key1=a, key2=b}(无序)
}
void putAll(H key, Map< ? extends HK, ? extends HV > m);
    @Test
public void testPutAll(){
Map<String,Object> param = new HashMap<String,Object>();
param.put("key1", "a");
param.put("key2", "b");
param.put("key3", "c");
opsForHash.putAll("he2", param);
System.out.println(opsForHash.entries("he2"));//{key2=b, key1=a, key3=c}
}
Long delete(H key, Object… hashKeys);
    @Test
public void testDelete(){
Map<String,Object> param = new HashMap<String,Object>();
param.put("key1", "a");
param.put("key2", "b");
param.put("key3", "c");
opsForHash.putAll("he3", param);
System.out.println(opsForHash.entries("he3"));//{key3=c, key2=b, key1=a}
opsForHash.delete("he3", "key1");
System.out.println(opsForHash.entries("he3"));//{key2=b, key3=c}
}
Boolean hasKey(H key, Object hashKey);
    @Test
public void testHashKey(){
Map<String,Object> param = new HashMap<String,Object>();
param.put("key1", "a");
param.put("key2", "b");
param.put("key3", "c");
opsForHash.putAll("he4", param);
System.out.println(opsForHash.hasKey("he", "key2"));//false
System.out.println(opsForHash.hasKey("he4", "key4"));//false
System.out.println(opsForHash.hasKey("he4", "key2"));//true
}
HV get(H key, Object hashKey);
    @Test
public void testGet(){
Map<String,Object> param = new HashMap<String,Object>();
param.put("key1", "a");
param.put("key2", "b");
param.put("key3", "c");
opsForHash.putAll("he5", param);
System.out.println(opsForHash.get("he5", "key1"));//a
System.out.println(opsForHash.get("he5", "key"));//null
}
List< HV > multiGet(H key, Collection< HK > hashKeys);
    @Test
public void testMultiGet(){
Map<String,Object> param = new HashMap<String,Object>();
param.put("key1", "a");
param.put("key2", "b");
param.put("key3", "c");
opsForHash.putAll("he6", param);
List<String> keys = new ArrayList<String>();
keys.add("key1");
keys.add("key");
keys.add("key2");
System.out.println(opsForHash.multiGet("he6", keys));//[a, null, b]
}
Long increment(H key, HK hashKey, long delta);
Double increment(H key, HK hashKey, double delta);
    @Test
public void testIncrement(){
Map<String,Object> param = new HashMap<String,Object>();
param.put("key1", "a");
param.put("key2", "b");
param.put("key3", "c");
param.put("key4", 4);
opsForHash.putAll("he7", param);
System.out.println(opsForHash.increment("he7", "key4", 1));//
System.out.println(opsForHash.increment("he7", "key4", 1.1));//6.1
try {
opsForHash.increment("he7", "key1", 1);//ERR hash value is not an integer
} catch (Exception e) {
e.printStackTrace();
}
try {
opsForHash.increment("he7", "key1", 1.1);//ERR hash value is not a float
} catch (Exception e) {
e.printStackTrace();
}
}
Set< HK > keys(H key);
    @Test
public void testKeys(){
redisTemplate.delete("he8");
Map<String,Object> param = new HashMap<String,Object>();
param.put("key4", "d");
param.put("key1", "a");
param.put("key3", "c");
param.put("key5", "e");
param.put("key2", "b");
opsForHash.putAll("he8", param);
Set<String> keys = opsForHash.keys("he8");
System.out.println(keys);//[key4, key3, key5, key2, key1]
}
Long size(H key);
    @Test
public void testSize(){
Map<String,Object> param = new HashMap<String,Object>();
param.put("key4", "d");
param.put("key1", "a");
param.put("key3", "c");
param.put("key5", "e");
param.put("key2", "b");
opsForHash.putAll("he9", param);
System.out.println(opsForHash.size("he9"));//
}
Boolean putIfAbsent(H key, HK hashKey, HV value);
    @Test
public void testPutIfAbsent(){
//仅当hashKey不存在时才设置散列hashKey的值。
System.out.println(opsForHash.putIfAbsent("he10", "key1", "a"));//true
System.out.println(opsForHash.putIfAbsent("he10", "key1", "a"));//false
}
List< HV > values(H key);
    @Test
public void testValues(){
Map<String,Object> param = new HashMap<String,Object>();
param.put("key4", "d");
param.put("key1", "a");
param.put("key3", "c");
param.put("key5", "e");
param.put("key2", "b");
opsForHash.putAll("he11", param);
List<Object> values = opsForHash.values("he11");
System.out.println(values);//[d, c, e, b, a]
}
Cursor< Map.Entry< HK, HV >> scan(H key, ScanOptions options);
    @Test
public void testScan(){
Map<String,Object> param = new HashMap<String,Object>();
param.put("key4", "d");
param.put("key1", "a");
param.put("key3", "c");
param.put("key5", "e");
param.put("key2", "b");
opsForHash.putAll("he13", param);
Cursor<Map.Entry<String, Object>> curosr = opsForHash.scan("he13", ScanOptions.NONE);
while(curosr.hasNext()){
Map.Entry<String, Object> entry = curosr.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
/**
key4:d
key3:c
key5:e
key2:b
key1:a
*/
}

转载自:https://blog.csdn.net/weixin_37490221/article/details/78135036

最新文章

  1. PRML
  2. 【读书笔记】iOS-内存释放
  3. mycat未配置对应表导致报错
  4. HashCode作用
  5. Java基础--访问权限控制符
  6. Mybatis拦截器介绍
  7. HDU5780 gcd 欧拉函数
  8. char 和 varchar
  9. 关于WINDOWS命令
  10. VC++ SetLayeredWindowAttributes 部分窗口透明鼠标穿透
  11. HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)
  12. 使用Python脚本操作MongoDB的教程
  13. jQuery系列 第六章 jQuery框架事件处理
  14. Java基础---Java---基础加强---类加载器、委托机制、AOP、 动态代理技术、让动态生成的类成为目标类的代理、实现Spring可配置的AOP框架
  15. 利用SVD-推荐未尝过的菜肴2
  16. springbootAdmin+eureka集群+swagger
  17. 转载:浅谈 Scala 中下划线的用途
  18. Linux:编译安装boost 1.69库
  19. 【题解】Luogu P5072 [Ynoi2015]盼君勿忘
  20. 题解——CF Manthan, Codefest 18 (rated, Div. 1 + Div. 2) T2(模拟)

热门文章

  1. debian下安装mysql 5.1.34
  2. 如何上传大文件到github上
  3. 图解 SQL 里的各种 JOIN
  4. python 并发编程 多进程 队列
  5. [转帖]探秘华为(一):华为和H3C(华三)的爱恨情仇史!
  6. 获取程序所有加载的dll名称
  7. 你知道 Java 类是如何被加载的吗?
  8. phpstudy添加PHP
  9. 并查集专题: HDU1232畅通工程
  10. oracle PL/SQL编程基础知识