RedisTemplate 的hash结构测试类, 使用场景 购物车

xiaoxiao2021-02-28  129

@Resource ( name = "redisTemplate" ) private HashOperations < String , String , Object > hashOps ; @Test public void test4 () throws Exception { String key = "test_User_shopping:" + 2 ; //id 2 的用户的购物车 ; hashOps . put ( key , "3" , 5 + "" ) ; hashOps . put ( key , "4" , 2 + "" ) ; hashOps . put ( key , "6" , 1 + "" ) ; Map < String , Object > map2 = hashOps . entries ( key ) ; // 返回 map 集合 // 用户购物车的列表为 ,3 号商品 5 ,4 2 ,6 1 ; hashOps . increment ( key , "6" , 2 ) ; // 用户再次添加商品 6 2 , Map < String , Object > objectMap = new HashMap <> () ; objectMap . put ( "7" , 3 + "" ) ; objectMap . put ( "9" , 3 + "" ) ; hashOps . putAll ( key , objectMap ) ; // 批量添加 Map < String , Object > map3 = hashOps . entries ( key ) ; // 返回 map 集合 hashOps . delete ( key , "6" ) ; // 移除商品 6 ; Map < String , Object > map4 = hashOps . entries ( key ) ; // 返回 map 集合 Boolean age1 = hashOps . hasKey ( key , "9" ) ; // 是否存在 hash 字段 Object age2 = hashOps . get ( key , "9" ) ; // 获取 hash 字段的值 ; Set < String > keys = hashOps . keys ( key ) ; // 返回 map key 集合 Set Long size = hashOps . size ( key ) ; // 返回列表的大小 , List < Object > values = hashOps . values ( key ) ; // 返回值的集合 ; List < String > hashkeys = new ArrayList <> () ; hashkeys . add ( "7" ) ; hashkeys . add ( "9" ) ; List < Object > objects = hashOps . multiGet ( key , hashkeys ) ; // 批量获取 , }
转载请注明原文地址: https://www.6miu.com/read-27079.html

最新回复(0)