提交
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package com.chaozhanggui.system.cashierservice.redis;
|
||||
|
||||
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.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
/**
|
||||
* @ClassName RedisConfig
|
||||
* @Author Administrator
|
||||
* @Date 2020/08/04
|
||||
* @Version 1.0
|
||||
* @Description
|
||||
*/
|
||||
@Configuration
|
||||
public class RedisConfig {
|
||||
|
||||
// 实例-单例模式
|
||||
private static RedisConfig redisConfig;
|
||||
|
||||
@Value("${spring.redis.host}")
|
||||
private String host;
|
||||
|
||||
@Value("${spring.redis.port}")
|
||||
private int port;
|
||||
|
||||
@Value("${spring.redis.timeout}")
|
||||
private int timeout;
|
||||
|
||||
@Value("${spring.redis.password}")
|
||||
private String password;
|
||||
|
||||
@Value("${spring.redis.jedis.pool.max-idle}")
|
||||
private int maxIdle;
|
||||
|
||||
@Value("${spring.redis.jedis.pool.max-wait}")
|
||||
private long maxWaitMillis;
|
||||
|
||||
@Value("${spring.redis.block-when-exhausted}")
|
||||
private boolean blockWhenExhausted;
|
||||
|
||||
@Value("${spring.redis.database}")
|
||||
private int database;
|
||||
|
||||
/**
|
||||
* 更换序列化器springSession默认序列化
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean("springSessionDefaultRedisSerializer")
|
||||
public RedisSerializer setSerializer() {
|
||||
return new GenericJackson2JsonRedisSerializer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JedisPool redisPoolFactory() {
|
||||
JedisPool jedisPool = new JedisPool();
|
||||
try {
|
||||
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
|
||||
jedisPoolConfig.setMaxIdle(maxIdle);
|
||||
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
|
||||
// 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
|
||||
jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
|
||||
// 是否启用pool的jmx管理功能, 默认true
|
||||
jedisPoolConfig.setJmxEnabled(true);
|
||||
jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout,password,database);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return jedisPool;
|
||||
}
|
||||
@Bean
|
||||
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
return container;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.chaozhanggui.system.cashierservice.redis;
|
||||
|
||||
/** 功能描述:redis前缀
|
||||
* @params:
|
||||
* @return:
|
||||
* @Author: wgc
|
||||
* @Date: 2020-12-19 15:02
|
||||
*/
|
||||
public class RedisCst {
|
||||
|
||||
//在线用户
|
||||
public static final String ONLINE_USER = "ONLINE_USER:";
|
||||
public static final String TABLE_CART = "TABLE:CART:";
|
||||
public static final String PRODUCT = "PRODUCT:";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,483 @@
|
||||
package com.chaozhanggui.system.cashierservice.redis;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @ClassName RedisUtil
|
||||
* @Author wgc
|
||||
* @Date 2019/12/9 14:28
|
||||
* @Version 1.0
|
||||
* @Description redis工具类
|
||||
*/
|
||||
@Component
|
||||
public class RedisUtil {
|
||||
// 成功标识
|
||||
private static final int REDIS_SUCCESS = 1;
|
||||
// 失败标识
|
||||
private static final int REDIS_FAILED = -1;
|
||||
@Value("${spring.redis.database}")
|
||||
private int database;
|
||||
|
||||
|
||||
@Autowired
|
||||
private JedisPool pool;
|
||||
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @param message
|
||||
* @return boolean
|
||||
* @Description: 保存StrinRedisConfigg信息
|
||||
* @author SLy
|
||||
* @date 2018-12-19 19:49
|
||||
*/
|
||||
public Integer saveMessage(String key, String message) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
return REDIS_FAILED;
|
||||
}
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
|
||||
jedis.set(key, message);
|
||||
return REDIS_SUCCESS;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return REDIS_FAILED;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 释放分布式锁
|
||||
* @param lockKey 锁
|
||||
* @param requestId 请求标识
|
||||
* @return 是否释放成功
|
||||
*/
|
||||
public boolean releaseDistributedLock(String lockKey, String requestId) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
|
||||
Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId));
|
||||
if ("true".equals(result)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @param message
|
||||
* @param expire
|
||||
* @return
|
||||
* @Description: 保存String信息(生命周期单位秒)
|
||||
* @author wgc
|
||||
* @date 2018-12-19 19:49
|
||||
*/
|
||||
public Integer saveMessage(String key, String message, int expire) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
return REDIS_FAILED;
|
||||
}
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
jedis.set(key, message);
|
||||
jedis.expire(key, expire);
|
||||
|
||||
return REDIS_SUCCESS;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return REDIS_FAILED;
|
||||
}
|
||||
public boolean exists(String key) {
|
||||
Jedis jedis = null;
|
||||
boolean flag = false;
|
||||
try {
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database != 0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
if (jedis.exists(key)) {
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
/**
|
||||
* @param key
|
||||
* @param message
|
||||
* @return
|
||||
* @Description: 保存byte[]信息
|
||||
* @author SLy
|
||||
* @date 2018-12-19 19:49
|
||||
*/
|
||||
public Integer saveMessage(String key, byte[] message) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
return REDIS_FAILED;
|
||||
}
|
||||
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
jedis.set(key.getBytes(), message);
|
||||
return REDIS_SUCCESS;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return REDIS_FAILED;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @return
|
||||
* @Description: 获取String对象类型值
|
||||
* @author SLy
|
||||
* @date 2018-12-19 19:49
|
||||
*/
|
||||
public String getMessage(String key) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
return null;
|
||||
}
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
// 获取jedis实例后可以对redis服务进行一系列的操作
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
String value = jedis.get(key);
|
||||
|
||||
return value;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @param key
|
||||
* @return
|
||||
* @Description: 获取String对象类型值
|
||||
* @author SLy
|
||||
* @date 2018-12-19 19:49
|
||||
*/
|
||||
public Set<String> getAllMessage(String key) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
return null;
|
||||
}
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
// 获取jedis实例后可以对redis服务进行一系列的操作
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
Set<String> value = jedis.keys(key);
|
||||
|
||||
return value;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @return
|
||||
* @Description: 通过key删除数据
|
||||
* @author SLy
|
||||
* @date 2018-12-19 19:49
|
||||
*/
|
||||
public Integer deleteByKey(String key) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
return REDIS_FAILED;
|
||||
}
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
jedis.del(key);
|
||||
return REDIS_SUCCESS;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return REDIS_FAILED;
|
||||
}
|
||||
/**
|
||||
* @param key
|
||||
* @return
|
||||
* @Description: 通过key删除数据
|
||||
* @author SLy
|
||||
* @date 2018-12-19 19:49
|
||||
*/
|
||||
public TreeSet<String> keys(String key) {
|
||||
Jedis jedis = null;
|
||||
TreeSet<String> keys = new TreeSet<>();
|
||||
try {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
return null;
|
||||
}
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
keys.addAll(jedis.keys(key));
|
||||
return keys;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public int getIncrementNum(String key, String message) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
String sss = jedis.get(key);
|
||||
if (StringUtils.isBlank(jedis.get(key))) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.add(Calendar.DAY_OF_YEAR, 1);
|
||||
cal.set(Calendar.HOUR_OF_DAY, 0);
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
cal.set(Calendar.MINUTE, 0);
|
||||
cal.set(Calendar.MILLISECOND, 0);
|
||||
long aa = (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
|
||||
int time = new Long(aa).intValue();
|
||||
jedis.set(key, message);
|
||||
jedis.expire(key, time);
|
||||
}
|
||||
jedis.incr(key);
|
||||
return REDIS_SUCCESS;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
|
||||
return REDIS_FAILED;
|
||||
}
|
||||
public int getIncrNum(String key, String message) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
if (message.equals("1")) {
|
||||
jedis.decr(key);
|
||||
}else {
|
||||
jedis.incr(key);
|
||||
}
|
||||
return REDIS_SUCCESS;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
|
||||
return REDIS_FAILED;
|
||||
}
|
||||
|
||||
public int getTicketNum(String key) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
jedis.incr(key);
|
||||
return REDIS_SUCCESS;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return REDIS_FAILED;
|
||||
}
|
||||
String secAddScript = "local prodid=KEYS[1];\r\n" +
|
||||
"local usernum=KEYS[2];\r\n" +
|
||||
"local num= redis.call(\"get\" ,prodid);\r\n" +
|
||||
" redis.call(\"SET\",prodid,tonumber(usernum)+tonumber(num));\r\n" +
|
||||
"return 1";
|
||||
public String secAdd(String key, String num) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
return REDIS_FAILED+"";
|
||||
}
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
Object result = jedis.eval(secAddScript, Arrays.asList(key,num), new ArrayList<>());
|
||||
String reString = String.valueOf(result);
|
||||
return reString;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return REDIS_FAILED+"";
|
||||
}
|
||||
|
||||
|
||||
|
||||
String secKillScript = "local prodid=KEYS[1];\r\n" +
|
||||
"local usernum=KEYS[2];\r\n" +
|
||||
"local num= redis.call(\"get\" ,prodid);\r\n" +
|
||||
"if tonumber(num)<tonumber(usernum) then \r\n" +
|
||||
" return 0;\r\n" +
|
||||
"end\r\n" +
|
||||
"if tonumber(num)<=0 then \r\n" +
|
||||
" return 0;\r\n" +
|
||||
"else \r\n" +
|
||||
" redis.call(\"DECRBY\",prodid,tonumber(usernum));\r\n" +
|
||||
"end\r\n" +
|
||||
"return 1";
|
||||
public String seckill(String key,String num) {
|
||||
Jedis jedis = null;
|
||||
try {
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
return REDIS_FAILED+"";
|
||||
}
|
||||
// 从jedis池中获取一个jedis实例
|
||||
jedis = pool.getResource();
|
||||
if (database!=0) {
|
||||
jedis.select(database);
|
||||
}
|
||||
Object result = jedis.eval(secKillScript, Arrays.asList( key,num), new ArrayList<>());
|
||||
String reString = String.valueOf(result);
|
||||
return reString;
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
// 释放对象池,即获取jedis实例使用后要将对象还回去
|
||||
if (jedis != null) {
|
||||
jedis.close();
|
||||
}
|
||||
}
|
||||
return REDIS_FAILED+"";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user