redis轻量级demo

<dependency>
        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-data-redisartifactId>dependency>spring:  redis:    host: 127.0.0.1    port: 6379    jedis:      pool:        max-active: 8        max-wait: -1        max-idle: 500        min-idle: 0

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    public  void put(String key, E value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public Boolean del(String key) {
        return redisTemplate.delete(key);
    }

    public  E getSingle(String key, Class cls) {
        Object res = redisTemplate.opsForValue().get(key);
        return cast(res, cls);
    }

    public  List getList(String key, Class cls) {
        Object res = redisTemplate.opsForValue().get(key);
        return castList(res, cls);
    }


    @SuppressWarnings("unchecked")
    private static  T cast(Object obj, Class cls) {
        return (T) obj;
    }


    @SuppressWarnings("unchecked")
    private static  List castList(Object obj, Class cls) {
        return (List) obj;
    }
}
    import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;import java.util.List;@Componentpublic class RedisUtil {    @Autowired    private RedisTemplate redisTemplate;    public  void put(String key, E value) {        redisTemplate.opsForValue().set(key, value);    }    public Boolean del(String key) {        return redisTemplate.delete(key);    }    public  E getSingle(String key, Class cls) {        Object res = redisTemplate.opsForValue().get(key);        return cast(res, cls);    }    public  List getList(String key, C

你可能感兴趣的:(redis)