redis应用详解

1、redis的5种数据结构类型:String(字符串)、Hash(哈希)、List(列表)、Set(集合)、Zset(有序集合)、

1.1 、String(字符串)

常用方法:

  • get:获取存储在给定key值中的value值
  • set:设置给定key值中的value值
  • del:删除给定key值中的value值
    ValueOperations<String, String> valueOperations = redisTemplate
                    .opsForValue();
            valueOperations.set("stringKey", "stringValue");
            logger.info("字符串set操作后,根据key值:stringKey,获取的value值为"
                    + valueOperations.get("stringKey"));
            valueOperations.getOperations().delete("stringKey");
            logger.info("字符串delete操作后,根据key值:stringKey,获取的value值为"
                    + valueOperations.get("stringKey"));  

调用打印结果

redis应用详解 

1.2、Hash(哈希)

 

常用方法:

  • Hset:在散列表里关联起给定的键值对
  • Hget:获取给定的散列键的值
  • Hgetall:获取散列包含的所有键值对
  • Hdel:如果给定键存在与散列表里面,则删除这个键
//操作hash
        HashOperations<String, String, String> hashOperations = redisTemplate
                .opsForHash();
        hashOperations.put("hashKey", "hashKey1", "hashValue1");
        hashOperations.put("hashKey", "hashKey2", "hashValue2");
        hashOperations.put("hashKey", "hashKey3", "hashValue3");
        logger.info("哈希put操作后,根据key值:hashKey,获取的所有hashKey值为"
                + hashOperations.keys("hashKey"));
        logger.info("哈希put操作后,根据key值:hashKey,获取的所有hashValue值为"
                + hashOperations.values("hashKey"));
        logger.info("哈希put操作后,根据key值:hashKey,获取的hashValue值为"
                + hashOperations.get("hashKey", "hashKey1"));
        hashOperations.getOperations().delete("hashKey");
        logger.info("哈希delete操作后,根据key值:hashKey,获取的hashValue值为"
                + hashOperations.get("hashKey", "hashKey1"));

调用打印结果

redis应用详解

 

 

 

 

 

 1.3、 List(列表)

上一篇:RedisTemplate操作Redis


下一篇:Redis实战应用:获取key值、批量获取key值、获取hashkey值、批量获取hashkey值等