zoukankan      html  css  js  c++  java
  • Spring Boot下Redis使用

    首先新建一个Spring Boot工程

    1.pom.xml增加jedis

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

      

    2. 增加RedisConfig配置文件

    @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);
        }
    }
    

      

    3. 增加配置文件

      

    4. 增加RedisClient类

    @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();
                }
            }
        }
    }
    

      

    5. 增加CacheController测试

    @Controller
    @RequestMapping("/cache")
    public class CacheController {
    
        @Autowired
        private  RedisClient redisClient;
    
        @RequestMapping("/set")
        @ResponseBody
        public  String set(@RequestParam("key") String key, @RequestParam("value") String value) throws Exception{
            redisClient.set(key,value);
            return  "Success";
        }
    
        @RequestMapping("/get")
        @ResponseBody
        public  String get(@RequestParam("key") String key) throws Exception{
           return   redisClient.get(key);
        }
    }
    

      

  • 相关阅读:
    [php代码]从svn获取指定版本,并同步到ftp上。
    java程序用pid重启
    Gearman安装,测试笔记
    ant编译android项目
    jquery代码收藏
    [读书笔记]读《code complete》有感
    无法解析的外部符号_main,该符号在函数_tmainCRTStartup中被引用
    4路电梯调度——pair program总结
    阅读作业2
    必应缤纷桌面的使用测试
  • 原文地址:https://www.cnblogs.com/linlf03/p/9656175.html
Copyright © 2011-2022 走看看