{"id":44118,"date":"2024-12-01T17:20:11","date_gmt":"2024-12-01T09:20:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/44118\/"},"modified":"2024-12-01T17:20:11","modified_gmt":"2024-12-01T09:20:11","slug":"redis%e5%ae%9e%e7%8e%b0%e8%ae%a2%e5%8d%95%e8%87%aa%e5%8a%a8%e8%bf%87%e6%9c%9f%e5%8a%9f%e8%83%bd%e7%9a%84%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/44118\/","title":{"rendered":"Redis\u5b9e\u73b0\u8ba2\u5355\u81ea\u52a8\u8fc7\u671f\u529f\u80fd\u7684\u793a\u4f8b\u4ee3\u7801"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u5b9e\u73b0\u8ba2\u5355\u81ea\u52a8\u8fc7\u671f\u529f\u80fd\u7684\u793a\u4f8b\u4ee3\u7801<\/h1>\n<p><span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aRedis\u5b9e\u73b0\u8ba2\u5355\u81ea\u52a8\u8fc7\u671f\u529f\u80fd\u7684\u793a\u4f8b\u4ee3\u7801\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<h2>\u524d\u8a00<br \/><\/h2>\n<p>\u7528\u6237\u4e0b\u5355\u540e\uff0c\u89c4\u5b9aXX\u5206\u949f\u540e\u81ea\u52a8\u8bbe\u7f6e\u4e3a\u201c\u5df2\u8fc7\u671f\u201d\uff0c\u4e0d\u80fd\u518d\u53d1\u8d77\u652f\u4ed8\u3002\u9879\u76ee\u7c7b\u4f3c\u6b64\u7c7b&#8221;\u8fc7\u671f&#8221;\u7684\u9700\u6c42\uff0c\u7b14\u8005\u63d0\u4f9b\u4e00\u79cd\u4f7f\u7528Redis\u7684\u89e3\u51b3\u601d\u8def\uff0c\u7ed3\u5408Redis\u7684\u8ba2\u9605\u3001\u53d1\u5e03\u548c\u952e\u7a7a\u95f4\u901a\u77e5\u673a\u5236\uff08Keyspace Notifications\uff09\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n<h2>\u914d\u7f6eredis.confg<br \/><\/h2>\n<p>notify-keyspace-events\u9009\u9879\u9ed8\u8ba4\u662f\u4e0d\u542f\u7528\uff0c\u6539\u4e3anotify-keyspace-events \u201cEx\u201d\u3002\u91cd\u542f\u751f\u6548\uff0c\u7d22\u5f15\u4f4di\u7684\u5e93\uff0c\u6bcf\u5f53\u6709\u8fc7\u671f\u7684\u5143\u7d20\u88ab\u5220\u9664\u65f6\uff0c\u5411**\u9891\u9053\u53d1\u9001\u901a\u77e5\u3002<br \/> E\u8868\u793a\u952e\u4e8b\u4ef6\u901a\u77e5\uff0c\u6240\u6709\u901a\u77e5\u4ee5\u4e3a\u524d\u7f00\uff1b<br \/> x\u8868\u793a\u8fc7\u671f\u4e8b\u4ef6\uff0c\u6bcf\u5f53\u6709\u8fc7\u671f\u88ab\u5220\u9664\u65f6\u53d1\u9001\u3002<\/p>\n<h2>\u4e0eSpringBoot\u8fdb\u884c\u96c6\u6210<br \/><\/h2>\n<p>\u2460\u6ce8\u518cJedisConnectionFactory<\/p>\n<pre>\nimport org.springframework.beans.factory.annotation.Value;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.data.redis.connection.RedisPassword;\nimport org.springframework.data.redis.connection.RedisStandaloneConfiguration;\nimport org.springframework.data.redis.connection.jedis.JedisConnectionFactory;\n\nimport redis.clients.jedis.JedisPool;\nimport redis.clients.jedis.JedisPoolConfig;\n\n@Configuration\npublic class RedisConfig {\n \n @Value(\"${redis.pool.maxTotal}\")\n private Integer maxTotal;\n \n @Value(\"${redis.pool.minIdle}\")\n private Integer minIdle;\n \n @Value(\"${redis.pool.maxIdle}\")\n private Integer maxIdle;\n \n @Value(\"${redis.pool.maxWaitMillis}\")\n private Integer maxWaitMillis;\n \n @Value(\"${redis.url}\")\n private String redisUrl;\n \n @Value(\"${redis.port}\")\n private Integer redisPort;\n \n @Value(\"${redis.timeout}\")\n private Integer redisTimeout;\n \n @Value(\"${redis.password}\")\n private String redisPassword;\n \n @Value(\"${redis.db.payment}\")\n private Integer paymentDataBase;\n \n private JedisPoolConfig jedisPoolConfig() {\n  JedisPoolConfig config = new JedisPoolConfig();\n  config.setMaxTotal(maxTotal);\n  config.setMinIdle(minIdle);\n  config.setMaxIdle(maxIdle);\n  config.setMaxWaitMillis(maxWaitMillis);\n  return config;\n }\n \n @Bean\n public JedisPool jedisPool() {\n  JedisPoolConfig config = this.jedisPoolConfig();\n  JedisPool jedisPool = new JedisPool(config, redisUrl, redisPort, redisTimeout, redisPassword);\n  return jedisPool;\n }\n \n @Bean(name = \"jedisConnectionFactory\")\n public JedisConnectionFactory jedisConnectionFactory() {\n  RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();\n  redisStandaloneConfiguration.setDatabase(paymentDataBase);\n  redisStandaloneConfiguration.setHostName(redisUrl);\n  redisStandaloneConfiguration.setPassword(RedisPassword.of(redisPassword));\n  redisStandaloneConfiguration.setPort(redisPort);\n\n  return new JedisConnectionFactory(redisStandaloneConfiguration);\n }\n}<\/pre>\n<p>\u2461\u6ce8\u518c\u76d1\u542c\u5668<\/p>\n<pre>\nimport org.springframework.data.redis.connection.Message;\nimport org.springframework.data.redis.connection.MessageListener;\nimport org.springframework.stereotype.Service;\nimport org.springframework.transaction.annotation.Transactional;\n\n@Service(value =\"paymentListener\")\npublic class PaymentListener implements MessageListener {\n\n @Override\n @Transactional\n public void onMessage(Message message, byte[] pattern) {\n  \/\/ \u8fc7\u671f\u4e8b\u4ef6\u5904\u7406\u6d41\u7a0b\n }\n}<\/pre>\n<p>\u2462\u914d\u7f6e\u8ba2\u9605\u5bf9\u8c61<\/p>\n<pre>\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.beans.factory.annotation.Qualifier;\nimport org.springframework.beans.factory.annotation.Value;\nimport org.springframework.boot.autoconfigure.AutoConfigureAfter;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.data.redis.connection.jedis.JedisConnectionFactory;\nimport org.springframework.data.redis.listener.PatternTopic;\nimport org.springframework.data.redis.listener.RedisMessageListenerContainer;\nimport org.springframework.data.redis.listener.adapter.MessageListenerAdapter;\n\n@Configuration\n@AutoConfigureAfter(value = RedisConfig.class)\npublic class PaymentListenerConfig {\n \n @Autowired\n @Qualifier(value = \"paymentListener\")\n private PaymentListener paymentListener;\n \n @Autowired\n @Qualifier(value = \"paymentListener\")\n private JedisConnectionFactory connectionFactory;\n \n @Value(\"${redis.db.payment}\")\n private Integer paymentDataBase;\n \n @Bean\n RedisMessageListenerContainer redisMessageListenerContainer(MessageListenerAdapter listenerAdapter) {\n        RedisMessageListenerContainer container = new RedisMessageListenerContainer();\n        container.setConnectionFactory(connectionFactory);\n        \/\/ \u76d1\u542cpaymentDataBase \u5e93\u7684\u8fc7\u671f\u4e8b\u4ef6\n        String subscribeChannel = \"__keyevent@\" + paymentDataBase + \"__:expired\";\n        container.addMessageListener(listenerAdapter, new PatternTopic(subscribeChannel));\n        return container;\n }\n \n @Bean\n    MessageListenerAdapter listenerAdapter() {\n        return new MessageListenerAdapter(paymentListener);\n    }\n}<\/pre>\n<p>paymentDataBase \u5e93\u5143\u7d20\u8fc7\u671f\u540e\u5c31\u4f1a\u8df3\u5165PaymentListener \u7684onMessage(Message message, byte[] pattern)\u65b9\u6cd5\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aRedis\u5b9e\u73b0\u8ba2\u5355\u81ea\u52a8\u8fc7\u671f\u529f\u80fd\u7684\u793a\u4f8b\u4ee3\u7801\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6570\u636e\u5e93\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\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   \u57fa\u4e8eRedis\u8fc7\u671f\u4e8b\u4ef6\u5b9e\u73b0\u8ba2\u5355\u8d85\u65f6\u53d6\u6d88\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Redis6.0\u642d\u5efa\u96c6\u7fa4Redis-cluster\u7684\u65b9\u6cd5\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5b9e\u73b0\u8ba2\u5355\u81ea\u52a8\u8fc7\u671f\u529f\u80fd\u7684\u793a\u4f8b\u4ee3\u7801 0\u6d4f\u89c8 \u6536\u85cf \u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300aRedis\u5b9e\u73b0\u8ba2\u5355\u81ea\u52a8\u8fc7\u671f\u529f\u80fd\u7684\u793a\u4f8b\u4ee3\u7801\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01 \u524d\u8a00 \u7528\u6237\u4e0b\u5355\u540e\uff0c\u89c4\u5b9aXX\u5206\u949f\u540e\u81ea\u52a8\u8bbe\u7f6e\u4e3a\u201c\u5df2\u8fc7\u671f\u201d\uff0c\u4e0d\u80fd\u518d\u53d1\u8d77\u652f\u4ed8\u3002\u9879\u76ee\u7c7b\u4f3c\u6b64\u7c7b&#8221;\u8fc7\u671f&#8221;\u7684\u9700\u6c42\uff0c\u7b14\u8005\u63d0\u4f9b\u4e00\u79cd\u4f7f\u7528Redis\u7684\u89e3\u51b3\u601d\u8def\uff0c\u7ed3\u5408Redis\u7684\u8ba2\u9605\u3001\u53d1\u5e03\u548c\u952e\u7a7a\u95f4\u901a\u77e5\u673a\u5236\uff08Keyspace Notifications\uff09\u8fdb\u884c\u5b9e\u73b0\u3002 \u914d\u7f6eredis.confg notify-keyspace-events\u9009\u9879\u9ed8\u8ba4\u662f\u4e0d\u542f\u7528\uff0c\u6539\u4e3anotify-keyspace-events \u201cEx\u201d\u3002\u91cd\u542f\u751f\u6548\uff0c\u7d22\u5f15\u4f4di\u7684\u5e93\uff0c\u6bcf\u5f53\u6709\u8fc7\u671f\u7684\u5143\u7d20\u88ab\u5220\u9664\u65f6\uff0c\u5411**\u9891\u9053\u53d1\u9001\u901a\u77e5\u3002 E\u8868\u793a\u952e\u4e8b\u4ef6\u901a\u77e5\uff0c\u6240\u6709\u901a\u77e5\u4ee5\u4e3a\u524d\u7f00\uff1b x\u8868\u793a\u8fc7\u671f\u4e8b\u4ef6\uff0c\u6bcf\u5f53\u6709\u8fc7\u671f\u88ab\u5220\u9664\u65f6\u53d1\u9001\u3002 \u4e0eSpringBoot\u8fdb\u884c\u96c6\u6210 \u2460\u6ce8\u518cJedisConnectionFactory import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisPassword; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @Configuration public class RedisConfig { @Value(&#8220;${redis.pool.maxTotal}&#8221;) private Integer maxTotal; @Value(&#8220;${redis.pool.minIdle}&#8221;) private Integer minIdle; @Value(&#8220;${redis.pool.maxIdle}&#8221;) private Integer maxIdle; @Value(&#8220;${redis.pool.maxWaitMillis}&#8221;) private Integer maxWaitMillis; @Value(&#8220;${redis.url}&#8221;) private String redisUrl; [&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-44118","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44118","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=44118"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44118\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=44118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=44118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=44118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}