feat: redis逻辑引入
This commit is contained in:
parent
2494af0a4f
commit
43b2559f6b
@ -23,7 +23,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@ServletComponentScan("com.yfd.platform.config")
|
@ServletComponentScan("com.yfd.platform.config")
|
||||||
@MapperScan(basePackages = {"com.yfd.platform.*.mapper","com.yfd.platform.*.*.mapper", "com.yfd.platform.common"})
|
@MapperScan(basePackages = {"com.yfd.platform.*.mapper","com.yfd.platform.*.*.mapper", "com.yfd.platform.common"})
|
||||||
//@ComponentScan("com.zny.dec")
|
//@ComponentScan("com.zny.dec")
|
||||||
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class, DataRedisAutoConfiguration.class})
|
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
|
||||||
//@SpringBootApplication
|
//@SpringBootApplication
|
||||||
@Import({DynamicDataSourceConfig.class})
|
@Import({DynamicDataSourceConfig.class})
|
||||||
@EnableCaching
|
@EnableCaching
|
||||||
|
|||||||
@ -0,0 +1,48 @@
|
|||||||
|
package com.yfd.platform.config;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||||
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RedisConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||||
|
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||||
|
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||||
|
objectMapper.registerModule(new JavaTimeModule());
|
||||||
|
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||||
|
|
||||||
|
Jackson2JsonRedisSerializer<Object> jsonSerializer = new Jackson2JsonRedisSerializer<>(objectMapper, Object.class);
|
||||||
|
|
||||||
|
StringRedisSerializer stringSerializer = new StringRedisSerializer();
|
||||||
|
|
||||||
|
redisTemplate.setKeySerializer(stringSerializer);
|
||||||
|
redisTemplate.setHashKeySerializer(stringSerializer);
|
||||||
|
redisTemplate.setValueSerializer(jsonSerializer);
|
||||||
|
redisTemplate.setHashValueSerializer(jsonSerializer);
|
||||||
|
|
||||||
|
redisTemplate.afterPropertiesSet();
|
||||||
|
return redisTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
|
||||||
|
stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||||
|
return stringRedisTemplate;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -7,12 +7,15 @@ import jakarta.annotation.Resource;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.web.cors.CorsConfiguration;
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||||
import org.springframework.web.filter.CorsFilter;
|
import org.springframework.web.filter.CorsFilter;
|
||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class WebConfig implements WebMvcConfigurer {
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
@Resource
|
@Resource
|
||||||
@ -21,11 +24,28 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
@Value("${app.zip-import.temp-dir}")
|
@Value("${app.zip-import.temp-dir}")
|
||||||
private String platformPath;
|
private String platformPath;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private StringRedisTemplate stringRedisTemplate;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public Cache<String, String> loginuserCache() {
|
public Cache<String, String> loginuserCache() {
|
||||||
return CacheUtil.newLRUCache(200);//用户登录缓存数 缺省200
|
return CacheUtil.newLRUCache(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putLoginCache(String key, String value, long timeoutSeconds) {
|
||||||
|
stringRedisTemplate.opsForValue().set(key, value, timeoutSeconds, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putLoginCache(String key, String value) {
|
||||||
|
stringRedisTemplate.opsForValue().set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLoginCache(String key) {
|
||||||
|
return stringRedisTemplate.opsForValue().get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeLoginCache(String key) {
|
||||||
|
stringRedisTemplate.delete(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|||||||
227
backend/src/main/java/com/yfd/platform/utils/RedisCacheUtil.java
Normal file
227
backend/src/main/java/com/yfd/platform/utils/RedisCacheUtil.java
Normal file
@ -0,0 +1,227 @@
|
|||||||
|
package com.yfd.platform.utils;
|
||||||
|
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class RedisCacheUtil {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private StringRedisTemplate stringRedisTemplate;
|
||||||
|
|
||||||
|
public void set(String key, Object value) {
|
||||||
|
redisTemplate.opsForValue().set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(String key, Object value, long timeout, TimeUnit unit) {
|
||||||
|
redisTemplate.opsForValue().set(key, value, timeout, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(String key, Object value, long timeoutSeconds) {
|
||||||
|
redisTemplate.opsForValue().set(key, value, timeoutSeconds, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> T get(String key, Class<T> clazz) {
|
||||||
|
Object value = redisTemplate.opsForValue().get(key);
|
||||||
|
if (value == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return (T) value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object get(String key) {
|
||||||
|
return redisTemplate.opsForValue().get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getString(String key) {
|
||||||
|
return stringRedisTemplate.opsForValue().get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setString(String key, String value) {
|
||||||
|
stringRedisTemplate.opsForValue().set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setString(String key, String value, long timeoutSeconds) {
|
||||||
|
stringRedisTemplate.opsForValue().set(key, value, timeoutSeconds, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setString(String key, String value, long timeout, TimeUnit unit) {
|
||||||
|
stringRedisTemplate.opsForValue().set(key, value, timeout, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean delete(String key) {
|
||||||
|
return redisTemplate.delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long delete(Collection<String> keys) {
|
||||||
|
return redisTemplate.delete(keys);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean exists(String key) {
|
||||||
|
return redisTemplate.hasKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean expire(String key, long timeout, TimeUnit unit) {
|
||||||
|
return redisTemplate.expire(key, timeout, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean expire(String key, long timeoutSeconds) {
|
||||||
|
return redisTemplate.expire(key, timeoutSeconds, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getExpire(String key) {
|
||||||
|
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long increment(String key, long delta) {
|
||||||
|
return redisTemplate.opsForValue().increment(key, delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long increment(String key) {
|
||||||
|
return redisTemplate.opsForValue().increment(key, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long decrement(String key, long delta) {
|
||||||
|
return redisTemplate.opsForValue().decrement(key, delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long decrement(String key) {
|
||||||
|
return redisTemplate.opsForValue().decrement(key, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void hSet(String key, String hashKey, Object value) {
|
||||||
|
redisTemplate.opsForHash().put(key, hashKey, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> T hGet(String key, String hashKey, Class<T> clazz) {
|
||||||
|
Object value = redisTemplate.opsForHash().get(key, hashKey);
|
||||||
|
if (value == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return (T) value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object hGet(String key, String hashKey) {
|
||||||
|
return redisTemplate.opsForHash().get(key, hashKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<Object, Object> hGetAll(String key) {
|
||||||
|
return redisTemplate.opsForHash().entries(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void hSetAll(String key, Map<String, Object> map) {
|
||||||
|
redisTemplate.opsForHash().putAll(key, map);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void hDelete(String key, Object... hashKeys) {
|
||||||
|
redisTemplate.opsForHash().delete(key, hashKeys);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean hExists(String key, String hashKey) {
|
||||||
|
return redisTemplate.opsForHash().hasKey(key, hashKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long hSize(String key) {
|
||||||
|
return redisTemplate.opsForHash().size(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Object> hKeys(String key) {
|
||||||
|
return redisTemplate.opsForHash().keys(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Object> hValues(String key) {
|
||||||
|
return redisTemplate.opsForHash().values(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long lPush(String key, Object... values) {
|
||||||
|
return redisTemplate.opsForList().leftPushAll(key, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long rPush(String key, Object... values) {
|
||||||
|
return redisTemplate.opsForList().rightPushAll(key, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object lPop(String key) {
|
||||||
|
return redisTemplate.opsForList().leftPop(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object rPop(String key) {
|
||||||
|
return redisTemplate.opsForList().rightPop(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Object> lRange(String key, long start, long end) {
|
||||||
|
return redisTemplate.opsForList().range(key, start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long lSize(String key) {
|
||||||
|
return redisTemplate.opsForList().size(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void lTrim(String key, long start, long end) {
|
||||||
|
redisTemplate.opsForList().trim(key, start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long sAdd(String key, Object... values) {
|
||||||
|
return redisTemplate.opsForSet().add(key, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Object> sMembers(String key) {
|
||||||
|
return redisTemplate.opsForSet().members(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean sIsMember(String key, Object value) {
|
||||||
|
return redisTemplate.opsForSet().isMember(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long sRemove(String key, Object... values) {
|
||||||
|
return redisTemplate.opsForSet().remove(key, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long sSize(String key) {
|
||||||
|
return redisTemplate.opsForSet().size(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean zAdd(String key, Object value, double score) {
|
||||||
|
return redisTemplate.opsForZSet().add(key, value, score);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Object> zRange(String key, long start, long end) {
|
||||||
|
return redisTemplate.opsForZSet().range(key, start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double zScore(String key, Object value) {
|
||||||
|
return redisTemplate.opsForZSet().score(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long zRemove(String key, Object... values) {
|
||||||
|
return redisTemplate.opsForZSet().remove(key, values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long zSize(String key) {
|
||||||
|
return redisTemplate.opsForZSet().size(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<String> keys(String pattern) {
|
||||||
|
return redisTemplate.keys(pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
// public void flushDb() {
|
||||||
|
// redisTemplate.execute((connection) -> {
|
||||||
|
// connection.flushDb();
|
||||||
|
// return null;
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user