`

springboot-第九章 SpringBoot2.x整合Redis实战

 
阅读更多

http://try.redis.io/ 可以去官网练习redis命令

 

https://redis.io/download 在官网上下载

 

 

Download, extract and compile Redis with:

$ wget http://download.redis.io/releases/redis-5.0.3.tar.gz
$ tar xzf redis-5.0.3.tar.gz
$ cd redis-5.0.3
$ make

The binaries that are now compiled are available in the src directory. Run Redis with:

$ src/redis-server

You can interact with Redis using the built-in client:

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

 

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
   <version>2.1.3.RELEASE</version>
</dependency>

 

spring.redis.database=2
spring.redis.host=10.138.60.119
spring.redis.port=6379
spring.redis.timeout=3000
#连接池中最大空闲链接,默认值是8
spring.redis.jedis.pool.max-idle=20
#连接池中最小空闲链接,默认是0
spring.redis.jedis.pool.min-idle=10
## 如果赋值为-1,则表示不限制;pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
spring.redis.jedis.pool.max-active=200
#等待连接的最长时间
spring.redis.jedis.pool.max-wait=1000

 

 

@RestController
@RequestMapping("/api/v1/redis")
public class RedisController {


    @Autowired
private RedisClient redis;
    @RequestMapping("/add")
    public  String add()
    {
            redis.set("name","kevin");
            return "success";
    }

    @RequestMapping("/get")
    public String get()
    {
        return redis.get("name");
    }
    @RequestMapping("/save_user")
    public boolean saveUser()
    {
        User user = new User("kevin","123",30,new Date());
        String usr = JsonUtils.obj2String(user);
        return redis.set("project:user:1001",usr);
    }
    @RequestMapping("/find_user")
    public String getUser()
    {
        return redis.get("project:user:1");
    }
}

 

public class RedisClient {

   @Autowired
private StringRedisTemplate redisTpl; //jdbcTemplate

public boolean set(String key ,String value){
      try{
         redisTpl.opsForValue().set(key, value);
         return true;
      }catch(Exception e){
         e.printStackTrace();
         return false;
      }
      
   }

   public String get(String key){
      return redisTpl.opsForValue().get(key);
   }
}
public class JsonUtils {

    private static ObjectMapper objectMapper = new ObjectMapper();
    
    //对象转字符串
public static <T> String obj2String(T obj){
        if (obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    //字符串转对象
public static <T> T string2Obj(String str,Class<T> clazz){
        if (StringUtils.isEmpty(str) || clazz == null){
            return null;
        }
        try {
            return clazz.equals(String.class)? (T) str :objectMapper.readValue(str,clazz);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics