{"id":201168,"date":"2025-05-10T10:42:25","date_gmt":"2025-05-10T02:42:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201168\/"},"modified":"2025-05-10T10:42:25","modified_gmt":"2025-05-10T02:42:25","slug":"redis%e5%9c%a8%e9%a1%b9%e7%9b%ae%e4%b8%ad%e7%9a%84%e4%bd%bf%e7%94%a8jedispool%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201168\/","title":{"rendered":"Redis\u5728\u9879\u76ee\u4e2d\u7684\u4f7f\u7528(JedisPool\u65b9\u5f0f)"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u5728\u9879\u76ee\u4e2d\u7684\u4f7f\u7528(JedisPool\u65b9\u5f0f)<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728\u6570\u636e\u5e93\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u5c31\u6574\u7406\u5206\u4eab\u300aRedis\u5728\u9879\u76ee\u4e2d\u7684\u4f7f\u7528(JedisPool\u65b9\u5f0f)\u300b\uff0c\u804a\u804a\u4f7f\u7528\u3001JedisPoolRedis\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<h2>springboot\u4e2dredis\u76f8\u5173\u914d\u7f6e<\/h2>\n<p>1\u3001pom.xml\u4e2d\u5f15\u5165\u4f9d\u8d56<\/p>\n<pre>\n\n   \n    \n     redis.clients\n    \n    \n     jedis\n    \n    \n     2.9.0\n    \n   <\/pre>\n<p>2\u3001springboot\u7684\u4e60\u60ef\u4f18\u4e8e\u914d\u7f6e\u3002\u4e5f\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u4e86application.yml\u6587\u4ef6\u914d\u7f6emysql\u7684\u57fa\u672c\u914d\u7f6e\u9879\u3002\u8fd9\u91cc\u4e5f\u5728application.yml\u91cc\u9762\u914d\u7f6eredis\u7684\u914d\u7f6e\u9879\u3002<\/p>\n<pre>\nspring:\n  datasource:\n        # \u9a71\u52a8\u914d\u7f6e\u4fe1\u606f\n        url: jdbc:mysql:\/\/localhost:3306\/spring_boot?useUnicode=true&amp;characterEncoding=utf8\n        username: root\n        password: root\n        type: com.alibaba.druid.pool.DruidDataSource\n        driver-class-name: com.mysql.jdbc.Driver\n\n        # \u8fde\u63a5\u6c60\u7684\u914d\u7f6e\u4fe1\u606f\n        filters: stat\n        maxActive: 20\n        initialSize: 1\n        maxWait: 60000\n        minIdle: 1\n        timeBetweenEvictionRunsMillis: 60000\n        minEvictableIdleTimeMillis: 300000\n        validationQuery: select 'x'\n        testWhileIdle: true\n        testOnBorrow: false\n        testOnReturn: false\n        poolPreparedStatements: true\n        maxOpenPreparedStatements: 20\n  redis:\n        host: 127.0.0.1\n        port: 6379\n        password: pass1234\n        pool:\n          max-active: 100\n          max-idle: 10\n          max-wait: 100000\n        timeout: 0<\/pre>\n<h2>springboot\u4e2dredis\u76f8\u5173\u7c7b<\/h2>\n<ul>\n<li>\u9879\u76ee\u64cd\u4f5credis\u662f\u4f7f\u7528\u7684RedisTemplate\u65b9\u5f0f\uff0c\u53e6\u5916\u8fd8\u53ef\u4ee5\u5b8c\u5168\u4f7f\u7528JedisPool\u548cJedis\u6765\u64cd\u4f5credis\u3002\u6574\u5408\u7684\u5185\u5bb9\u4e5f\u662f\u4ece\u7f51\u4e0a\u6536\u96c6\u6574\u5408\u800c\u6765\uff0c\u7f51\u4e0a\u6574\u5408\u7684\u65b9\u5f0f\u548c\u65b9\u6cd5\u975e\u5e38\u7684\u591a\uff0c\u6709\u4f7f\u7528\u6ce8\u89e3\u5f62\u5f0f\u7684\uff0c\u6709\u4f7f\u7528Jackson2JsonRedisSerializer\u6765\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316key value\u7684\u503c\u7b49\u7b49\uff0c\u5f88\u591a\u5f88\u591a\u3002\u8fd9\u91cc\u4f7f\u7528\u7684\u662f\u6211\u8ba4\u4e3a\u6bd4\u8f83\u5bb9\u6613\u7406\u89e3\u548c\u638c\u63e1\u7684\uff0c\u57fa\u4e8eJedisPool\u914d\u7f6e\uff0c\u4f7f\u7528RedisTemplate\u6765\u64cd\u4f5credis\u7684\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<p>redis\u5355\u72ec\u653e\u5728\u4e00\u4e2a\u5305redis\u91cc\uff0c\u5728\u5305\u91cc\u5148\u521b\u5efaRedisConfig.java\u6587\u4ef6\u3002<\/p>\n<p>RedisConfig.java<\/p>\n<pre>\n@Configuration\n@EnableAutoConfiguration\npublic class RedisConfig {\n\n    @Bean\n    @ConfigurationProperties(prefix = \"spring.redis.pool\")\n    public JedisPoolConfig getRedisConfig(){\n        JedisPoolConfig config = new JedisPoolConfig();\n        return config;\n    }\n\n    @Bean\n    @ConfigurationProperties(prefix = \"spring.redis\")\n    public JedisConnectionFactory getConnectionFactory() {\n        JedisConnectionFactory factory = new JedisConnectionFactory();\n        factory.setUsePool(true);\n        JedisPoolConfig config = getRedisConfig();\n        factory.setPoolConfig(config);\n        return factory;\n    }\n\n    @Bean\n    public RedisTemplate getRedisTemplate() {\n        JedisConnectionFactory factory = getConnectionFactory();\n        RedisTemplate template = new StringRedisTemplate(factory);\n        return template;\n    }\n\n}<\/pre>\n<ul>\n<li>\u5728\u5305\u91cc\u521b\u5efaRedisService\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7bRedisServiceImpl\uff0c\u8fd9\u4e2a\u7c7b\u5b9e\u73b0\u4e86\u63a5\u53e3\u7684\u6240\u6709\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>RedisServiceImpl.java<\/p>\n<pre>\n@Service(\"redisService\")\npublic class RedisServiceImpl implements RedisService {\n\n    @Resource\n    private RedisTemplate\n   \n     redisTemplate;\n\n    @Override\n    public boolean set(final String key, final String value) {\n        boolean result = redisTemplate.execute(new RedisCallback\n    \n     () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer\n     \n       serializer = redisTemplate.getStringSerializer(); connection.set(serializer.serialize(key), serializer.serialize(value)); return true; } }); return result; } @Override public String get(final String key) { String result = redisTemplate.execute(new RedisCallback\n      \n       () { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer\n       \n         serializer = redisTemplate.getStringSerializer(); byte[] value = connection.get(serializer.serialize(key)); return serializer.deserialize(value); } }); return result; } @Override public boolean expire(final String key, long expire) { return redisTemplate.expire(key, expire, TimeUnit.SECONDS); } @Override public boolean remove(final String key) { boolean result = redisTemplate.execute(new RedisCallback\n        \n         () { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer\n         \n           serializer = redisTemplate.getStringSerializer(); connection.del(key.getBytes()); return true; } }); return result; } }\n         \n        \n       \n      \n     \n    \n   <\/pre>\n<p>\u5728\u8fd9\u91ccexecute()\u65b9\u6cd5\u5177\u4f53\u7684\u5e95\u5c42\u6ca1\u6709\u53bb\u7814\u7a76\uff0c\u53ea\u77e5\u9053\u8fd9\u6837\u80fd\u5b9e\u73b0\u5bf9\u4e8eredis\u6570\u636e\u7684\u64cd\u4f5c\u3002<br \/> redis\u4fdd\u5b58\u7684\u6570\u636e\u4f1a\u5728\u5185\u5b58\u548c\u786c\u76d8\u4e0a\u5b58\u50a8\uff0c\u6240\u4ee5\u9700\u8981\u505a\u5e8f\u5217\u5316\uff1b\u8fd9\u4e2a\u91cc\u9762\u4f7f\u7528\u7684StringRedisSerializer\u6765\u505a\u5e8f\u5217\u5316\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u65b9\u5f0f\u7684\u6cdb\u578b\u6307\u5b9a\u7684\u662fString\uff0c\u53ea\u80fd\u4f20String\u8fdb\u6765\u3002\u6240\u4ee5\u9879\u76ee\u4e2d\u91c7\u7528json\u5b57\u7b26\u4e32\u505aredis\u7684\u4ea4\u4e92\u3002<\/p>\n<p>\u5230\u6b64\uff0credis\u5728springboot\u4e2d\u7684\u6574\u5408\u5df2\u7ecf\u5b8c\u6bd5\uff0c\u4e0b\u9762\u5c31\u6765\u6d4b\u8bd5\u4f7f\u7528\u4e00\u4e0b\u3002<\/p>\n<h2>5. springboot\u9879\u76ee\u4e2d\u4f7f\u7528redis<\/h2>\n<p>\u5728\u8fd9\u91cc\u5c31\u76f4\u63a5\u4f7f\u7528springboot\u9879\u76ee\u4e2d\u81ea\u5e26\u7684\u5355\u5143\u6d4b\u8bd5\u7c7bSpringbootApplicationTests\u8fdb\u884c\u6d4b\u8bd5\u3002<\/p>\n<pre>\n@RunWith(SpringRunner.class)\n@SpringBootTest\npublic class SpringbootApplicationTests {\n\n    private JSONObject json = new JSONObject();\n\n    @Autowired\n    private RedisService redisService;\n\n    @Test\n    public void contextLoads() throws Exception {\n    }\n\n\n    \/**\n     * \u63d2\u5165\u5b57\u7b26\u4e32\n     *\/\n    @Test\n    public void setString() {\n        redisService.set(\"redis_string_test\", \"springboot redis test\");\n    }\n\n    \/**\n     * \u83b7\u53d6\u5b57\u7b26\u4e32\n     *\/\n    @Test\n    public void getString() {\n        String result = redisService.get(\"redis_string_test\");\n        System.out.println(result);\n    }\n\n    \/**\n     * \u63d2\u5165\u5bf9\u8c61\n     *\/\n    @Test\n    public void setObject() {\n        Person person = new Person(\"person\", \"male\");\n        redisService.set(\"redis_obj_test\", json.toJSONString(person));\n    }\n\n    \/**\n     * \u83b7\u53d6\u5bf9\u8c61\n     *\/\n    @Test\n    public void getObject() {\n        String result = redisService.get(\"redis_obj_test\");\n        Person person = json.parseObject(result, Person.class);\n        System.out.println(json.toJSONString(person));\n    }\n\n    \/**\n     * \u63d2\u5165\u5bf9\u8c61List\n     *\/\n    @Test\n    public void setList() {\n        Person person1 = new Person(\"person1\", \"male\");\n        Person person2 = new Person(\"person2\", \"female\");\n        Person person3 = new Person(\"person3\", \"male\");\n        List\n   \n     list = new ArrayList();\n        list.add(person1);\n        list.add(person2);\n        list.add(person3);\n        redisService.set(\"redis_list_test\", json.toJSONString(list));\n    }\n\n    \/**\n     * \u83b7\u53d6list\n     *\/\n    @Test\n    public void getList() {\n        String result = redisService.get(\"redis_list_test\");\n        List\n    \n      list = json.parseArray(result, String.class); System.out.println(list); } @Test public void remove() { redisService.remove(\"redis_test\"); } } class Person { private String name; private String sex; public Person() { } public Person(String name, String sex) { this.name = name; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }\n    \n   <\/pre>\n<p>\u5728\u8fd9\u91cc\u5148\u662f\u7528@Autowired\u6ce8\u89e3\u628aredisService\u6ce8\u5165\u8fdb\u6765\uff0c\u7136\u540e\u7531\u4e8e\u662f\u4f7f\u7528json\u5b57\u7b26\u4e32\u8fdb\u884c\u4ea4\u4e92\uff0c\u6240\u4ee5\u5f15\u5165fastjson\u7684JSONObject\u7c7b\u3002\u7136\u540e\u4e3a\u4e86\u65b9\u4fbf\uff0c\u76f4\u63a5\u5728\u8fd9\u4e2a\u6d4b\u8bd5\u7c7b\u91cc\u9762\u52a0\u4e86\u4e00\u4e2aPerson\u7684\u5185\u90e8\u7c7b\u3002<\/p>\n<p>\u4e00\u5171\u6d4b\u8bd5\u4e86\uff1a\u5bf9\u4e8estring\u7c7b\u578b\u7684\u5b58\u53d6\uff0c\u5bf9\u4e8eobject\u7c7b\u578b\u7684\u5b58\u53d6\uff0c\u5bf9\u4e8elist\u7c7b\u578b\u7684\u5b58\u53d6\uff0c\u5176\u5b9e\u672c\u8d28\u90fd\u662f\u8f6c\u6210\u4e86json\u5b57\u7b26\u4e32\u3002\u8fd8\u6709\u5c31\u662f\u6839\u636ekey\u6765\u6267\u884cremove\u64cd\u4f5c\u3002<\/p>\n<p>\u83b7\u53d6\u5b57\u7b26\u4e32\uff1a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167240707363aee82113205.png\" class=\"aligncenter\"><\/p>\n<p>\u83b7\u53d6\u5bf9\u8c61\uff1a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167240707363aee82182b49.png\" class=\"aligncenter\"><\/p>\n<p>\u83b7\u53d6list\uff1a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167240707363aee821b12e9.png\" class=\"aligncenter\"><\/p>\n<p>redis\u7ba1\u7406\u5ba2\u6237\u7aef\u6570\u636e\uff1a<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167240707363aee821e914d.png\" class=\"aligncenter\"><\/p>\n<p>\u5230\u6b64\uff0c\u6d4b\u8bd5\u5b8c\u6210\uff0c\u5bf9\u4e8e\u5e38\u7528\u7684\u4e00\u4e9b\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u5b58\u53d6\u64cd\u4f5c\u4e5f\u57fa\u672c\u8c03\u8bd5\u901a\u8fc7\u3002\u6240\u4ee5\u672c\u6587\u5bf9\u4e8espringboot\u6574\u5408redis\u5230\u6b64\u7ed3\u675f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5728\u9879\u76ee\u4e2d\u7684\u4f7f\u7528(Jed&#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-201168","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201168","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=201168"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201168\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}