Commit e3081be2 authored by 张辰's avatar 张辰

redis cache

parent f43ac197
package com.tanpu.community.cache;
import com.alibaba.fastjson.JSON;
import com.tanpu.common.redis.RedisHelper;
import com.tanpu.common.redis.RedisKeyHelper;
import com.tanpu.community.util.SpringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.util.List;
import java.util.function.Supplier;
public class RedisCache {
private String cacheName;
private RedisHelper redisHelper;
public<T> T getObject(String key, Integer expireSeconds, Supplier<T> func, Class<T> clz) {
String value = get(key);
// todo 考虑缓存穿透的问题.
if (StringUtils.isNotBlank(value)) {
return JSON.parseObject(value, clz);
}
T ret = func.get();
put(key, ret, expireSeconds);
return ret;
}
public<T> List<T> getList(String key, Integer expireSeconds, Supplier<List<T>> func, Class<T> clz) {
String value = get(key);
if (StringUtils.isNotBlank(value)) {
return JSON.parseArray(value, clz);
}
List<T> ret = func.get();
put(key, ret, expireSeconds);
return ret;
}
private String get(String key) {
key = cacheName + ":" + key;
return redisHelper.get(key);
}
private void put(String key, Object obj, Integer expireSeconds) {
if (obj == null) return;
key = cacheName + ":" + key;
String value = JSON.toJSONString(obj);
if (expireSeconds == 0) {
redisHelper.set(key, value);
} else {
redisHelper.set(key, value, Duration.ofSeconds(expireSeconds));
}
}
public static class Builder {
RedisCache cache = new RedisCache();
public Builder cacheName(String cacheName) {
cache.cacheName = cacheName;
cache.redisHelper = SpringUtils.getBean(RedisHelper.class);
}
public RedisCache build() {
return cache;
}
}
}
package com.tanpu.community.config;
import com.tanpu.community.cache.RedisCache;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CacheConfig {
@Bean
public RedisCache redisCache() {
return new RedisCache.Builder().cacheName("community2").build();
}
}
package com.tanpu.community.controller;public class SearchController {
}
package com.tanpu.community.controller;
import com.tanpu.community.manager.KafkaManager;
import com.tanpu.community.service.UserInfoService;
import com.tanpu.community.service.VisitSummaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@Slf4j
@RequestMapping(value = "/api/test")
public class TestController {
@Autowired
private KafkaManager kafkaManager;
@Autowired
private VisitSummaryService visitSummaryService;
@Autowired
private UserInfoService userInfoService;
@GetMapping(value = "/1")
@ResponseBody
public Object sendKafka() {
return userInfoService.queryUserById("USER_INFO15912897523789");
}
}
package com.tanpu.community.manager;public class SearchManager {
}
package com.tanpu.community.service.base;public class ESService {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment