Category:数据库
Article From:https://www.cnblogs.com/williamjie/p/9121908.html

The last note Reddis is integrated, and the operation Redis uses RedisTemplate, but in practice, there are a lot of people used to use JedisPool and Jedis to operate Redis, and the following uses Jedis integration examples.

 

Modify the RedisConfig class as follows:

 

[java] view plain copy

  1. package com.vic.config;  
  2.   
  3. import org.apache.log4j.Logger;  
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  5. import org.springframework.boot.context.properties.ConfigurationProperties;  
  6. import org.springframework.context.annotation.Bean;  
  7. import org.springframework.context.annotation.Configuration;  
  8.   
  9. import redis.clients.jedis.JedisPool;  
  10. import redis.clients.jedis.JedisPoolConfig;  
  11.   
  12. /** 
  13.  *  
  14.  * @author vic 
  15.  * @desc redis config bean 
  16.  * 
  17.  */  
  18. @Configuration  
  19. @EnableAutoConfiguration  
  20. @ConfigurationProperties(prefix = “spring.redis”, locations = “classpath:application.properties”)  
  21. public class RedisConfig {  
  22.   
  23.     private static Logger logger = Logger.getLogger(RedisConfig.class);  
  24.       
  25.     private String hostName;  
  26.   
  27.     private int port;  
  28.   
  29.     private String password;  
  30.   
  31.     private int timeout;  
  32.       
  33.     @Bean  
  34.     public JedisPoolConfig getRedisConfig(){  
  35.         JedisPoolConfig config = new JedisPoolConfig();  
  36.         return config;  
  37.     }  
  38.       
  39.     @Bean  
  40.     public JedisPool getJedisPool(){  
  41.         JedisPoolConfig config = getRedisConfig();  
  42.         JedisPool pool = new JedisPool(config,hostName,port,timeout,password);  
  43.         logger.info(“init JredisPool …”);  
  44.         return pool;  
  45.     }  
  46.   
  47.     public String getHostName() {  
  48.         return hostName;  
  49.     }  
  50.   
  51.     public void setHostName(String hostName) {  
  52.         this.hostName = hostName;  
  53.     }  
  54.   
  55.     public int getPort() {  
  56.         return port;  
  57.     }  
  58.   
  59.     public void setPort(int port) {  
  60.         this.port = port;  
  61.     }  
  62.   
  63.     public String getPassword() {  
  64.         return password;  
  65.     }  
  66.   
  67.     public void setPassword(String password) {  
  68.         this.password = password;  
  69.     }  
  70.   
  71.     public int getTimeout() {  
  72.         return timeout;  
  73.     }  
  74.   
  75.     public void setTimeout(int timeout) {  
  76.         this.timeout = timeout;  
  77.     }  
  78. }  

Because the JedisPool instantiate object is to pass the parameters of host, password, and other parameters to the construction, so here the whole RedisConfig is defined as a configuration class, and the configuration attributes such as host, password, etc. are defined, through springBoot attribute file is automatically injected.

 

Then look at how to use in Service:

Modify the IRedisService interface:

 

 

[java] view plain copy

  1. /** 
  2.  *  
  3.  * @author vic 
  4.  * @desc redis service 
  5.  */  
  6. public interface IRedisService {  
  7.   
  8.     public Jedis getResource();  
  9.   
  10.     public void returnResource(Jedis jedis);  
  11.   
  12.     public void set(String key, String value);  
  13.   
  14.     public String get(String key);  
  15.   
  16. }  

 

RedisServiceImplement class code:

 

[java] view plain copy

  1. package com.vic.service.impl;  
  2.   
  3. import org.apache.log4j.Logger;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.stereotype.Service;  
  6.   
  7. import com.vic.service.IRedisService;  
  8.   
  9. import redis.clients.jedis.Jedis;  
  10. import redis.clients.jedis.JedisPool;  
  11.   
  12. /** 
  13.  *  
  14.  * @author vic 
  15.  * @desc resdis service 
  16.  * 
  17.  */  
  18. @Service  
  19. public class RedisServiceImpl implements IRedisService {  
  20.       
  21.     private static Logger logger = Logger.getLogger(RedisServiceImpl.class);  
  22.   
  23.     @Autowired  
  24.     private JedisPool jedisPool;  
  25.       
  26.     @Override  
  27.     public Jedis getResource() {  
  28.         return jedisPool.getResource();  
  29.     }  
  30.   
  31.     @SuppressWarnings(“deprecation”)  
  32.     @Override  
  33.     public void returnResource(Jedis jedis) {  
  34.         if(jedis != null){  
  35.             jedisPool.returnResourceObject(jedis);  
  36.         }  
  37.     }  
  38.   
  39.     @Override  
  40.     public void set(String key, String value) {  
  41.         Jedis jedis=null;  
  42.         try{  
  43.             jedis = getResource();  
  44.             jedis.set(key, value);  
  45.             logger.info(“Redis set success – ” + key + “, value:” + value);  
  46.         } catch (Exception e) {  
  47.             e.printStackTrace();  
  48.             logger.error(“Redis set error: “+ e.getMessage() +” – ” + key + “, value:” + value);  
  49.         }finally{  
  50.             returnResource(jedis);  
  51.         }  
  52.     }  
  53.       
  54.     @Override  
  55.     public String get(String key) {  
  56.         String result = null;  
  57.         Jedis jedis=null;  
  58.         try{  
  59.             jedis = getResource();  
  60.             result = jedis.get(key);  
  61.             logger.info(“Redis get success – ” + key + “, value:” + result);  
  62.         } catch (Exception e) {  
  63.             e.printStackTrace();  
  64.             logger.error(“Redis set error: “+ e.getMessage() +” – ” + key + “, value:” + result);  
  65.         }finally{  
  66.             returnResource(jedis);  
  67.         }  
  68.         return result;  
  69.     }  
  70.   
  71. }  

JedisPoolThe object is automatically injected, manually acquiring Jedis objects for Redis operation, and ExampleController testing:

[java] view plain copy

  1. @RequestMapping(“/redis/set”)  
  2. public ResponseModal redisSet(@RequestParam(“value”)String value){  
  3.     redisService.set(“name”, value);  
  4.     return new ResponseModal(200, true, “success”, null);  
  5. }  
  6.   
  7. @RequestMapping(“/redis/get”)  
  8. public ResponseModal redisGet(){  
  9.     String name = redisService.get(“name”);  
  10.     return new ResponseModal(200, true,“success”,name);  
  11. }  

 

 

Test URL:http://localhost:8080/redis/set? Value=vic?

 

{"code":200,"success":true,"message":"success","response":null}

Test URL:http://localhost:8080/redis/get response results:

 

 

{"code":200,"success":true,"message":"success","response":"vic"}

Click the download example

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *