第一步 : 导入pom文件
org.springframework.boot spring-boot-starter-data-redis
第二步 : 配置我们的redis连接
spring.redis.host: 192.168.1.60 spring.redis.database: 5 spring.redis.password: 123456
示例 :
//导入redis实现类
@Autowired
private RedisTemplate redisTemplate;@Test
void contextLoads() {
redisTemplate.opsForValue().set("key","112233");
System.out.printf(redisTemplate.opsForValue().get("key")+"");
//opsForValue 操作字符串
//opsForHash 操作哈希
//opsForList 操作集合
//opsForSet 操作set
//....................
//以上方法后面直接点出我们的命令就可以直接使用了
//上面是操作数据的方法,一些基本的常用方法我们都是可以直接使用的,比如事物,提交等等等等
redisTemplate.exec()
redisTemplate.multi()
// ................................//当然还有一些特殊命令
RedisConnection tory = redisTemplate.getConnectionFactory().getConnection();
tory.flushDb();
tory.flushAll();
}
下面我们使用工具类操作我们的Redis :
public class StringRedisTemplate extends RedisTemplate{ public StringRedisTemplate() { this.setKeySerializer(RedisSerializer.string()); this.setValueSerializer(RedisSerializer.string()); this.setHashKeySerializer(RedisSerializer.string()); this.setHashValueSerializer(RedisSerializer.string()); } public StringRedisTemplate(RedisConnectionFactory connectionFactory) { this(); this.setConnectionFactory(connectionFactory); this.afterPropertiesSet(); } protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) { return new DefaultStringRedisConnection(connection); } }
package com.odcchina.ctp.server.api.util;
import com.odcchina.commonutil.Md5Util;
import com.odcchina.ctp.server.api.SpringContextHolder;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class RedisUtil {
//根据自己项目的方式注入ben
private final static StringRedisTemplate stringRedisTemplate = SpringContextHolder.getBean("stringRedisTemplate");
public static Set keys(String pattern) {
return stringRedisTemplate.keys(pattern);
}
public static void del(String key) {
stringRedisTemplate.delete(key);
}
public static void delByPattern(String pattern) {
Set keySet = keys(pattern);
stringRedisTemplate.delete(keySet);
}
public static boolean expire(String key, long seconds) {
return stringRedisTemplate.expire(key, seconds, TimeUnit.SECONDS);
}
public static long ttl(String key) {
return stringRedisTemplate.getExpire(key, TimeUnit.SECONDS);
}
public static boolean persist(String key) {
return stringRedisTemplate.persist(key);
}
/// String 操作
public static void set(String key, String value) {
ValueOperations op = stringRedisTemplate.opsForValue();
op.set(key, value);
}
public static void setEx(String key, String value, long seconds) {
set(key, value);
expire(key, seconds);
}
public static boolean setNewNx(String key,String value,long seconds ){
ValueOperations op = stringRedisTemplate.opsForValue();
return op.setIfAbsent(key, value, Duration.ofSeconds(seconds));
}
public static boolean setNx(String key, String value) {
ValueOperations op = stringRedisTemplate.opsForValue();
return op.setIfAbsent(key, value);
}
public static void getLock(String key){
getLock(key, 2L);
}
public static void getLock(String key, long expireSecond) {
if (expireSecond > 10) {
expireSecond = 2;
}
String md5Key = "fifiLock:" + key + ":" + Md5Util.md5(key);
String value = "0";
while (!setNx(md5Key, value)) {
sleep(20);
}
expire(md5Key, expireSecond);
}
public static void unLock(String key) {
String md5Key = "FIFILock:" + key + ":" + Md5Util.md5(key);
del(md5Key);
}
public static void sleep(int timeMills) {
try {
Thread.sleep(timeMills);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static long incrBy(String key, long num) {
ValueOperations op = stringRedisTemplate.opsForValue();
return op.increment(key, num);
}
public static String get(String key) {
ValueOperations op = stringRedisTemplate.opsForValue();
return op.get(key);
}
/// list操作
public static void lPush(String key, String... values) {
ListOperations listOp = stringRedisTemplate.opsForList();
listOp.leftPushAll(key, values);
}
public static String rPop(String key) {
ListOperations listOp = stringRedisTemplate.opsForList();
return listOp.rightPop(key);
}
public static int lLen(String key) {
ListOperations opsForList = stringRedisTemplate.opsForList();
return opsForList.size(key).intValue();
}
public static List lRange(String key, int start, int end) {
ListOperations opsForList = stringRedisTemplate.opsForList();
return opsForList.range(key, start, end);
}
/// hash
/// set
/// sorted set
public static void setList(String key, List list){
ListOperations opsForList = stringRedisTemplate.opsForList();
opsForList.leftPushAll(key, list);
}
public static String hGet(String key, String item) {
HashOperations mapOp = stringRedisTemplate.opsForHash();
return mapOp.get(key, item);
}
public static Map hmGet(String key) {
HashOperations mapOp = stringRedisTemplate.opsForHash();
return mapOp.entries(key);
}
public static boolean hmSet(String key, Map map){
try {
HashOperations mapOp = stringRedisTemplate.opsForHash();
mapOp.putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean hmset(String key, Map map, long time) {
try {
HashOperations mapOp = stringRedisTemplate.opsForHash();
mapOp.putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean hset(String key, String item, String value) {
try {
HashOperations mapOp = stringRedisTemplate.opsForHash();
mapOp.put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean hset(String key,String item,Object value,long time) {
try {
HashOperations mapOp = stringRedisTemplate.opsForHash();
mapOp.put(key, item, value);
if(time>0){
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void hdel(String key, Object... item){
HashOperations mapOp = stringRedisTemplate.opsForHash();
mapOp.delete(key,item);
}
public static boolean hHasKey(String key, String item){
HashOperations mapOp = stringRedisTemplate.opsForHash();
return mapOp.hasKey(key, item);
}
public static double hincr(String key, String item,double by){
HashOperations mapOp = stringRedisTemplate.opsForHash();
return mapOp.increment(key, item, by);
}
public static double hdecr(String key, String item,double by){
HashOperations mapOp = stringRedisTemplate.opsForHash();
return mapOp.increment(key, item,-by);
}
}



