依赖
<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>
yml文件配置
spring:
redis:
database: 0
host: 127.0.0.1
port: 6379
配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(jedisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}
redis对字符串数据的通用操作类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* redis对字符串数据的通用操作类
*/
@Component
public class RedisMapper {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 根据键保存字符串
*
* @param key
* @param val
* @return
*/
public boolean setKey(String key, String val) {
redisTemplate.opsForValue().set(key, val);
if (redisTemplate.hasKey(key)) {
return true;
} else {
return false;
}
}
/**
* 根据键删除
*
* @param key
* @return
*/
public boolean delKey(String key) {
if (redisTemplate.delete(key)) {
return true;
} else {
return false;
}
}
/**
* 添加并设置过期时间
*
* @param key
* @param val
* @param timeout
* @param unit
* @return
*/
public boolean setKey(String key, String val, long timeout, TimeUnit unit) {
redisTemplate.opsForValue().set(key, val, timeout, unit);
if (redisTemplate.hasKey(key)) {
return true;
} else {
return false;
}
}
/**
* 根据键获得值
*
* @param key
* @return
*/
public Object getKey(String key) {
if (redisTemplate.hasKey(key)) {
Object value = redisTemplate.opsForValue().get(key);
return value;
} else {
return null;
}
}
/**
* 保存hash数据到redis
*
* @param key1 大键
* @param key2 小键
* @param value 值,如果是对象使用json字符串
* @return
*/
public boolean setHash(String key1, String key2, String value) {
try {
redisTemplate.opsForHash().put(key1, key2, value);
return true;
} catch (Exception e) {
return false;
}
}
/**
* 取出reids中的hash数据
*
* @param key1 大键
* @param key2 小键
* @return
*/
public String getHash(String key1, String key2) {
String result = null;
try {
result = redisTemplate.opsForHash().get(key1, key2).toString();
return result;
} catch (Exception e) {
return null;
}
}
/**
* 根据大键取出hash中所有的数据
*
* @param key
* @return
*/
public Map<Object, Object> getHashEntries(String key) {
Map<Object, Object> map = redisTemplate.opsForHash().entries(key);
if (map != null) {
return map;
}
return null;
}
/**
* 判断指定的key是否存在
*
* @param key
* @return
*/
public Boolean hasKey(String key) {
try {
if (redisTemplate.hasKey(key)) {
return true;
}
} catch (Exception e) {
return false;
}
return false;
}
/**
* 删除has中指定的键
*
* @param key1 大键
*@param key2 小键
* @return
*/
public boolean delHasKey(String key1, String key2) {
try {
Long n = redisTemplate.opsForHash().delete(key1, key2);
if (n > 0) {
return true;
}else {
return false;
}
} catch (Exception e) {
return false;
}
}
}