2022-03-31初始化

This commit is contained in:
djz236
2022-03-31 16:51:44 +08:00
commit 8fe9b95f86
98 changed files with 8758 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yyd-base</artifactId>
<groupId>com.yyd</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yyd-base-redis</artifactId>
<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<!--引入redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,107 @@
package com.yyd.base.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author dimengzhe
* @date 2020/9/18 19:37
* @description
*/
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-active}")
private int redisPoolMaxActive;
@Value("${spring.redis.jedis.pool.max-wait}")
private int redisPoolMaxWait;
@Value("${spring.redis.jedis.pool.max-idle}")
private int redisPoolMaxIdle;
@Value("${spring.redis.jedis.pool.min-idle}")
private int redisPoolMinIdle;
@Value("${spring.redis.database}")
private int database;
@Bean
public JedisPoolConfig getJedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//最大空闲数
jedisPoolConfig.setMaxIdle(redisPoolMaxIdle);
//最小空闲数
jedisPoolConfig.setMinIdle(redisPoolMinIdle);
//最大建立连接等待时间
jedisPoolConfig.setMaxWaitMillis(redisPoolMaxWait);
//连接池的最大数据库连接数
jedisPoolConfig.setMaxTotal(redisPoolMaxActive);
return jedisPoolConfig;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
jedisConnectionFactory.setDatabase(database);
//IP地址
jedisConnectionFactory.setHostName(host);
//如果redis设置有密码
jedisConnectionFactory.setPassword(password);
//端口号
jedisConnectionFactory.setPort(port);
//客户端超时时间单位是毫秒
jedisConnectionFactory.setTimeout(timeout);
// jedisConnectionFactory.afterPropertiesSet(); //记得添加这行!
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
//redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setKeySerializer(stringRedisSerializer);
// hash的key也采用String的序列化方式
redisTemplate.setHashKeySerializer(stringRedisSerializer);
// value序列化方式采用jackson
// redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setValueSerializer(stringRedisSerializer);
// hash的value序列化方式采用jackson
redisTemplate.setHashValueSerializer(stringRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}

View File

@@ -0,0 +1,322 @@
package com.yyd.base.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author dimengzhe
* @date 2020/9/9 17:35
* @description redis工具类
*/
@Service
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
/**
* 字符串类型:根据key设置value值,如果key中的value存在,那么返回false
*
* @param key
* @param value
* @return
*/
public Boolean setnx(final String key, final String value, final long expration, final TimeUnit timeUnit) {
return (Boolean) redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
RedisSerializer<String> redisSerializer = redisTemplate.getStringSerializer();
byte keys[] = redisSerializer.serialize(key);
byte values[] = redisSerializer.serialize(value);
return redisConnection.set(keys, values, Expiration.from(expration, timeUnit), RedisStringCommands.SetOption.SET_IF_ABSENT);
}
});
}
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, final String value) {
boolean result = (boolean) redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
connection.set(serializer.serialize(key), serializer.serialize(value));
return true;
}
});
return result;
}
/**
* 写入缓存设置时效时间
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public boolean expire(String key, long expire) {
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
/**
* 读取缓存
*
* @param key
* @return
*/
public String get(final String key) {
String result = (String) redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte[] value = connection.get(serializer.serialize(key));
return serializer.deserialize(value);
}
});
return result;
}
/**
* 正则获取key集合
*
* @param pattern
* @return
*/
public Set<String> keys(String pattern) {
Set<String> keys = redisTemplate.keys(pattern);
return keys;
}
/**
* 批量删除对应的value
*
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* 批量删除key
*
* @param pattern
*/
public void removePattern(final String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0) {
redisTemplate.delete(keys);
}
}
public Long remove(final String key) {
return (Long) redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte keys[] = serializer.serialize(key);
return redisConnection.del(keys);
}
});
}
/**
* 判断缓存中是否有对应的value
*
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 哈希 添加
*
* @param key
* @param hashKey
* @param value
*/
public void hmSet(String key, Object hashKey, Object value) {
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.put(key, hashKey, value);
}
/**
* 哈希获取数据
*
* @param key
* @param hashKey
* @return
*/
public String hmGet(String key, Object hashKey) {
HashOperations<String, String, String> hash = redisTemplate.opsForHash();
return hash.get(key, hashKey);
}
/**
* 获取哈希 keys
*
* @param key
* @return
*/
public Set<String> hmGetKeys(String key) {
HashOperations<String, String, String> hash = redisTemplate.opsForHash();
return hash.keys(key);
}
/**
* 删除集合中的key
*
* @param key
* @param hashKey
*/
public void hmDelete(String key, Object hashKey) {
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.delete(key, hashKey);
}
/**
* 列表添加
*
* @param k
* @param v
*/
public void lPush(String k, Object v) {
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush(k, v);
}
/**
* 列表获取
*
* @param k
* @param l
* @param l1
* @return
*/
public List<Object> lRange(String k, long l, long l1) {
ListOperations<String, Object> list = redisTemplate.opsForList();
return list.range(k, l, l1);
}
/**
* 集合添加
*
* @param key
* @param value
*/
public void add(String key, Object value) {
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add(key, value);
}
/**
* 集合获取
*
* @param key
* @return
*/
public Set<Object> setMembers(String key) {
SetOperations<String, Object> set = redisTemplate.opsForSet();
return set.members(key);
}
/**
* 有序集合添加
*
* @param key
* @param value
* @param scoure
*/
public void zAdd(String key, Object value, double scoure) {
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add(key, value, scoure);
}
/**
* 有序集合获取
*
* @param key
* @param scoure
* @param scoure1
* @return
*/
public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
/**
* 实现命令:TTL key 以秒为单位返回给定key的剩余生存时间
*
* @param key
* @return
*/
public long ttl(String key) {
return redisTemplate.getExpire(key);
}
/**
* @param keyPrefix: key前缀
* @description: 根据前缀获取所有的符合该keyvalue值
* @author: dimengzhe
* @date: 2021/11/6 16:16
* @return: java.util.List<java.lang.String>
*/
public List<String> getValuesByPrefix(String keyPrefix) {
List<String> keys = getKeysByPrefix(keyPrefix);
List<Object> values = redisTemplate.opsForValue().multiGet(keys);
return (List<String>) (List) values;
}
/**
* @param keyPrefix: key前缀
* @description: 根据前缀获取所有的符合该key的所有key值集合
* @author: dimengzhe
* @date: 2021/11/6 16:17
* @return: java.util.List<java.lang.String>
*/
public List<String> getKeysByPrefix(String keyPrefix) {
Set<String> keys = redisTemplate.keys(keyPrefix + "*");
return Arrays.asList(keys.toArray(new String[0]));
}
}