首先是Jedis的Bean工厂
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisBeanFactory {
private static final String HOST = "localhost";
private static final int PORT = 6379;
private static final String PASSWORD = "devroot";
private static final int SELECT_DB = 0;
public static Jedis JEDIS;
public static JedisPool JEDIS_POOL;
static {
Jedis jedis = new Jedis(HOST, PORT);
jedis.auth(PASSWORD);
jedis.select(SELECT_DB);
JEDIS = jedis;
JEDIS_POOL = new JedisPool(new JedisPoolConfig(), HOST, PORT, 10000, PASSWORD);
}
public static Jedis get() {
return JEDIS;
}
public static Jedis get(String host, int port, String password, int selectDB) {
Jedis jedis = new Jedis(
host,
port
);
jedis.auth(password);
jedis.select(selectDB);
return jedis;
}
public static Jedis getByConnectionPool() {
Jedis jedis = JEDIS_POOL.getResource();
jedis.select(SELECT_DB);
return jedis;
}
}
缓存尝试接口
public interface TryToGetFromCache <T>{
T hit(String key);
T miss(String key);
default T query(String key,int seconds ){
T hit = this.hit(key);
if ( hit instanceof String && StringUtils.isNotBlank((String)hit)) {
return hit;
}
T miss = miss(key);
if ( miss instanceof String && StringUtils.isNotBlank((String)miss)) {
Jedis jedis = JedisBeanFactory.get();
jedis.set(key, JSONObject.toJSONString(miss));
jedis.expire(key,seconds);
return miss;
}
return miss;
}
}
使用
public class CacheServiceImpl {
public static void main(String[] args) {
CacheServiceImpl cacheService = new CacheServiceImpl();
String query = cacheService.query();
System.out.println(query);
}
public String query(){
TryToGetFromCache<String> getFromCache = new TryToGetFromCache<String>() {
@Override
public String hit(String key) {
return JedisBeanFactory.get().get(key);
}
@Override
public String miss(String key) {
return "张三!";
}
};
String result = getFromCache.query("AName", 20);
System.out.println(result);
return result;
}
}
Json转map使用这个方法可以赋予泛型
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/88e535c524b76f369501a48ecaab5766.png)
改为Lambda写法
@FunctionalInterface
public interface LambdaGetFromCache<T>{
T miss();
default T query(String key,Class<T> resultClass,int seconds){
Jedis jedis = JedisBeanFactory.get();
String redisJson = jedis.get(key);
if (StringUtils.isNotBlank(redisJson)) {
try {
return JSONObject.parseObject(redisJson,resultClass);
} catch (Exception e) {
e.printStackTrace();
}
}
T miss = miss();
if (ObjectUtil.isNotEmpty(miss)) {
jedis.set(key, JSONObject.toJSONString(miss));
jedis.expire(key,seconds);
return miss;
}
return miss;
}
}
Lambda写法的使用
public class CacheServiceImpl {
public static void main(String[] args) {
CacheServiceImpl cacheService = new CacheServiceImpl();
Weapon weapon = cacheService.query();
System.out.println(weapon);
}
public Weapon query() {
LambdaGetFromCache<Weapon> getFromCache = () -> Weapon.builder()
.name("AR-15")
.code("ar15rDIAS")
.price(998)
.build();
Weapon result = getFromCache.query("Weapon",Weapon.class, 30);
return result;
}
}