码迷,mamicode.com
首页 > 其他好文 > 详细

redis 初步使用

时间:2020-02-23 22:34:18      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:value   factory   port   key   enc   cep   import   finally   body   

<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.8.2</version>
</dependency>

import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPool; @Configuration public class RedisConfig { @Bean(name = "redisPool") public JedisPool jedisPool(@Value("${jedis.host}") String host, @Value("${jedis.port}") int port) { return new JedisPool(host, port); } } import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import javax.annotation.Resource; // http://redis.cn/ @Component public class RedisClient { @Resource(name = "redisPool") private JedisPool jedisPool; public void set(String key, String value) throws Exception { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.set(key, value); } finally { if (jedis != null) { jedis.close(); } } } public String get(String key) throws Exception { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.get(key); } finally { if (jedis != null) { jedis.close(); } } } }

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/cache") public class CacheController { @Autowired private RedisClient redisClient; @RequestMapping("/set") @ResponseBody public String set(@RequestParam("k") String k, @RequestParam("v") String v) throws Exception { redisClient.set(k, v); return "SUCCESS"; } @RequestMapping("/get") @ResponseBody public String get(@RequestParam("k") String k) throws Exception { return redisClient.get(k); } }

技术图片

 

redis 初步使用

标签:value   factory   port   key   enc   cep   import   finally   body   

原文地址:https://www.cnblogs.com/EarlyBridVic/p/12354508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!