{"id":44357,"date":"2024-12-01T15:51:03","date_gmt":"2024-12-01T07:51:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/44357\/"},"modified":"2024-12-01T15:51:03","modified_gmt":"2024-12-01T07:51:03","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:\/\/fwq.ai\/blog\/44357\/","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><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<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<p>\u4eca\u5929\u5173\u4e8e\u300aRedis&nbsp;key-value\u4e71\u7801\u7684\u89e3\u51b3\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8eredis\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff01<\/p>\n<p>    \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1a\u811a\u672c\u4e4b\u5bb6 \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u6d45\u8c08Redis\u7f13\u51b2\u533a\u673a\u5236\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u901a\u8fc7redis\u51cf\u5e93\u5b58\u7684\u79d2\u6740\u573a\u666f\u5b9e\u73b0\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Redis&nbsp;key-value\u4e71\u7801\u7684\u89e3\u51b3 0\u6d4f\u89c8 \u6536\u85cf \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 redis \u914d\u7f6e\u7c7b import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringBootConfiguration; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @SpringBootConfiguration public class RedisConfig extends CachingConfigurerSupport { &nbsp; &nbsp; \/** &nbsp; &nbsp; &nbsp;* \u6ce8\u5165 RedisConnectionFactory &nbsp; &nbsp; &nbsp;*\/ &nbsp; &nbsp; @Autowired &nbsp; &nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-44357","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=44357"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44357\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=44357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=44357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=44357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}