{"id":201536,"date":"2025-05-10T11:59:59","date_gmt":"2025-05-10T03:59:59","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201536\/"},"modified":"2025-05-10T11:59:59","modified_gmt":"2025-05-10T03:59:59","slug":"redis-key-value%e4%b9%b1%e7%a0%81%e7%9a%84%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201536\/","title":{"rendered":"Redis\u00a0key-value\u4e71\u7801\u7684\u89e3\u51b3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis&nbsp;key-value\u4e71\u7801\u7684\u89e3\u51b3<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6765\u5230golang\u5b66\u4e60\u7f51\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6570\u636e\u5e93\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aRedis&nbsp;key-value\u4e71\u7801\u7684\u89e3\u51b3\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\u4e71\u7801\u3001Rediskey-value\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>redis \u914d\u7f6e\u7c7b<\/p>\n<pre>import com.fasterxml.jackson.annotation.JsonAutoDetect;\nimport com.fasterxml.jackson.annotation.PropertyAccessor;\nimport com.fasterxml.jackson.databind.ObjectMapper;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.SpringBootConfiguration;\nimport org.springframework.cache.CacheManager;\nimport org.springframework.cache.annotation.CachingConfigurerSupport;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.data.redis.cache.RedisCacheManager;\nimport org.springframework.data.redis.connection.RedisConnectionFactory;\nimport org.springframework.data.redis.core.RedisTemplate;\nimport org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;\nimport org.springframework.data.redis.serializer.StringRedisSerializer;\n\n@SpringBootConfiguration\npublic class RedisConfig extends CachingConfigurerSupport {\n\n&nbsp; &nbsp; \/**\n&nbsp; &nbsp; &nbsp;* \u6ce8\u5165 RedisConnectionFactory\n&nbsp; &nbsp; &nbsp;*\/\n&nbsp; &nbsp; @Autowired\n&nbsp; &nbsp; private RedisConnectionFactory redisConnectionFactory;\n\n&nbsp; &nbsp; @Bean\n&nbsp; &nbsp; public CacheManager cacheManager(RedisConnectionFactory factory) {\n&nbsp; &nbsp; &nbsp; &nbsp; return RedisCacheManager.builder(factory).build();\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; @Bean\n&nbsp; &nbsp; public RedisTemplate\n   \n     functionDomainRedisTemplate() {\n&nbsp; &nbsp; &nbsp; &nbsp; RedisTemplate\n    \n      redisTemplate = new RedisTemplate(); &nbsp; &nbsp; &nbsp; &nbsp; redisTemplate.setConnectionFactory(redisConnectionFactory); &nbsp; &nbsp; &nbsp; &nbsp; \/\/ \u4f7f\u7528Jackson2JsonRedisSerialize \u66ff\u6362\u9ed8\u8ba4\u5e8f\u5217\u5316 &nbsp; &nbsp; &nbsp; &nbsp; Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); &nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper objectMapper = new ObjectMapper(); &nbsp; &nbsp; &nbsp; &nbsp; objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); &nbsp; &nbsp; &nbsp; &nbsp; objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL); &nbsp; &nbsp; &nbsp; &nbsp; jackson2JsonRedisSerializer.setObjectMapper(objectMapper); &nbsp; &nbsp; &nbsp; &nbsp; \/\/ \u8bbe\u7f6evalue\u7684\u5e8f\u5217\u5316\u89c4\u5219\u548c key\u7684\u5e8f\u5217\u5316\u89c4\u5219 &nbsp; &nbsp; &nbsp; &nbsp; StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); &nbsp; &nbsp; &nbsp; &nbsp; redisTemplate.setKeySerializer(stringRedisSerializer); &nbsp; &nbsp; &nbsp; &nbsp; redisTemplate.setHashKeySerializer(stringRedisSerializer); &nbsp; &nbsp; &nbsp; &nbsp; redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); &nbsp; &nbsp; &nbsp; &nbsp; redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); &nbsp; &nbsp; &nbsp; &nbsp; redisTemplate.afterPropertiesSet(); &nbsp; &nbsp; &nbsp; &nbsp; return redisTemplate; &nbsp; &nbsp; } }\n    \n   <\/pre>\n<p>\u5f53\u4f7f\u7528opsForValue() \u5b58\u53d6String\u7c7b\u578bkey\uff0cvalue\u60c5\u5f62<\/p>\n<pre>    @Autowired\n    private StringRedisTemplate redisTemplate;<\/pre>\n<p>\u5f53\u4f7f\u7528opsForValue() \u5b58\u53d6String\u7c7b\u578bkey\uff0c\u81ea\u5b9a\u4e49\u5bf9\u8c61value\u60c5\u5f62<\/p>\n<pre>    @Autowired\n    private RedisTemplate\n   \n     redisTemplate;\n   <\/pre>\n<p>\u5f53\u4f7f\u7528hash\u7ed3\u6784\u65f6<\/p>\n<pre>    @Autowired\n    private RedisTemplate\n   \n     redisTemplate;\n\n   <\/pre>\n<pre>BoundHashOperations\n   \n     ops = redisTemplate.boundHashOps(\"key1\");\n        ops.put(\"key2\",obj);\n\n   <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis&nbsp;key-v&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201536","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201536"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201536\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}