commit init

This commit is contained in:
weihongyang
2022-06-20 16:26:51 +08:00
commit 7aaa6700b3
171 changed files with 9178 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
package com.cnbm.common.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import lombok.extern.log4j.Log4j2;
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.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
/**
* @Author weihongyang
* @Date 2022/6/7 3:00 PM
* @Version 1.0
*/
@Configuration
@Log4j2
public class RedisConfig {
@Resource
private RedisConnectionFactory factory;
@Bean
public Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(){
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
return jackson2JsonRedisSerializer;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
}

View File

@@ -0,0 +1,58 @@
package com.cnbm.common.redis;
/**
* @Author weihongyang
* @Date 2022/6/7 3:01 PM
* @Version 1.0
*/
public class RedisKeys {
/**
* 系统参数Key
*/
public static String getSysParamsKey(){
return "sys:params";
}
/**
* 验证码Key
*/
public static String getCaptchaKey(String uuid){
return "sys:captcha:" + uuid;
}
/**
* 登录用户Key
*/
public static String getSecurityUserKey(Long id){
return "sys:security:user:" + id;
}
/**
* 系统日志Key
*/
public static String getSysLogKey(){
return "sys:log";
}
/**
* 系统资源Key
*/
public static String getSysResourceKey(){
return "sys:resource";
}
/**
* 用户菜单导航Key
*/
public static String getUserMenuNavKey(Long userId){
return "sys:user:nav:" + userId;
}
/**
* 用户权限标识Key
*/
public static String getUserPermissionsKey(Long userId){
return "sys:user:permissions:" + userId;
}
}

View File

@@ -0,0 +1,118 @@
package com.cnbm.common.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @Author weihongyang
* @Date 2022/6/7 3:00 PM
* @Version 1.0
*/
@Component
public class RedisUtils {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/** 默认过期时长为24小时单位秒 */
public final static long DEFAULT_EXPIRE = 60 * 60 * 24L;
/** 过期时长为1小时单位秒 */
public final static long HOUR_ONE_EXPIRE = 60 * 60 * 1L;
/** 过期时长为6小时单位秒 */
public final static long HOUR_SIX_EXPIRE = 60 * 60 * 6L;
/** 不设置过期时长 */
public final static long NOT_EXPIRE = -1L;
public void set(String key, Object value, long expire){
redisTemplate.opsForValue().set(key, value);
if(expire != NOT_EXPIRE){
expire(key, expire);
}
}
public void set(String key, Object value){
set(key, value, DEFAULT_EXPIRE);
}
public Object get(String key, long expire) {
Object value = redisTemplate.opsForValue().get(key);
if(expire != NOT_EXPIRE){
expire(key, expire);
}
return value;
}
public Object get(String key) {
return get(key, NOT_EXPIRE);
}
public void delete(String key) {
redisTemplate.delete(key);
}
public void delete(Collection<String> keys) {
redisTemplate.delete(keys);
}
public Object hGet(String key, String field) {
return redisTemplate.opsForHash().get(key, field);
}
public Map<String, Object> hGetAll(String key){
HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
return hashOperations.entries(key);
}
public void hMSet(String key, Map<String, Object> map){
hMSet(key, map, DEFAULT_EXPIRE);
}
public void hMSet(String key, Map<String, Object> map, long expire){
redisTemplate.opsForHash().putAll(key, map);
if(expire != NOT_EXPIRE){
expire(key, expire);
}
}
public void hSet(String key, String field, Object value) {
hSet(key, field, value, DEFAULT_EXPIRE);
}
public void hSet(String key, String field, Object value, long expire) {
redisTemplate.opsForHash().put(key, field, value);
if(expire != NOT_EXPIRE){
expire(key, expire);
}
}
public void expire(String key, long expire){
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
public void hDel(String key, Object... fields){
redisTemplate.opsForHash().delete(key, fields);
}
public void leftPush(String key, Object value){
leftPush(key, value, DEFAULT_EXPIRE);
}
public void leftPush(String key, Object value, long expire){
redisTemplate.opsForList().leftPush(key, value);
if(expire != NOT_EXPIRE){
expire(key, expire);
}
}
public Object rightPop(String key){
return redisTemplate.opsForList().rightPop(key);
}
}