Pom文件
<!--springboot- 整合redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
yml文件
spring:
redis:
host: 49.235.155.13
port: 6379
password: bbbbbccc
timeout: 5000
maxIdle: 20
maxActive: -1
maxTotal: 100
maxWaitMillis: 100
minEvictableIdleTimeMillis: 864000000
numTestsPerEvictionRun: 10
timeBetweenEvictionRunsMillis: 300000
testOnBorrow: true
testWhileIdle: false
database: 1
配置redis序列化方法
package com.jy.chat_status_server.common.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
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();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
写一个工具类
package com.jy.chat_status_server.common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class RedisUtil {
private static Logger logger = LoggerFactory.getLogger(RedisUtil.class);
private static RedisTemplate redisTemplate = (RedisTemplate)ApplicationHelper.getBean("redisTemplate");
public static boolean expire(String key,long time){
try {
if(time>0){
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
logger.error("RedisUtils expire(String key,long time) failure."+e.getMessage());
return false;
}
}
public static long getExpire(String key){
return redisTemplate.getExpire(key,TimeUnit.SECONDS);
}
public static boolean hasKey(String key){
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
logger.error("RedisUtils hasKey(String key) failure."+e.getMessage());
return false;
}
}
@SuppressWarnings("unchecked")
public static void del(String ... key){
if(key!=null&&key.length>0){
if(key.length==1){
redisTemplate.delete(key[0]);
}else{
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
public static Object get(String key){
return key==null?null:redisTemplate.opsForValue().get(key);
}
public static boolean set(String key,Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
logger.error("RedisUtils set(String key,Object value) failure."+e.getMessage());
return false;
}
}
public static boolean set(String key,Object value,long time){
try {
if(time>0){
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
}else{
set(key, value);
}
return true;
} catch (Exception e) {
logger.error("RedisUtils set(String key,Object value,long time) failure."+e.getMessage());
return false;
}
}
public static long incr(String key, long delta){
if(delta<0){
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
public static long decr(String key, long delta){
if(delta<0){
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
public static Object hget(String key,String item){
return redisTemplate.opsForHash().get(key, item);
}
public static Map<Object,Object> hmget(String key){
return redisTemplate.opsForHash().entries(key);
}
public static boolean hmset(String key, Map<String,Object> map){
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
logger.error("RedisUtils hmset(String key, Map<String,Object> map) failure."+e.getMessage());
return false;
}
}
public static boolean hmset(String key, Map<String,Object> map, long time){
try {
redisTemplate.opsForHash().putAll(key, map);
if(time>0){
expire(key, time);
}
return true;
} catch (Exception e) {
logger.error("RedisUtils hmset(String key, Map<String,Object> map, long time) failure."+e.getMessage());
return false;
}
}
public static boolean hset(String key,String item,Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
logger.error("RedisUtils hset(String key,String item,Object value) failure."+e.getMessage());
return false;
}
}
public static boolean hset(String key,String item,Object value,long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if(time>0){
expire(key, time);
}
return true;
} catch (Exception e) {
logger.error("RedisUtils hset(String key,String item,Object value,long time) failure."+e.getMessage());
return false;
}
}
public static void hdel(String key, Object... item){
redisTemplate.opsForHash().delete(key,item);
}
public static boolean hHasKey(String key, String item){
return redisTemplate.opsForHash().hasKey(key, item);
}
public static double hincr(String key, String item,double by){
return redisTemplate.opsForHash().increment(key, item, by);
}
public static double hdecr(String key, String item,double by){
return redisTemplate.opsForHash().increment(key, item,-by);
}
public static Set<Object> sGet(String key){
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
logger.error("RedisUtils sGet(String key) failure."+e.getMessage());
return null;
}
}
public static boolean sHasKey(String key,Object value){
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
logger.error("RedisUtils sHasKey(String key,Object value) failure."+e.getMessage());
return false;
}
}
public static long sSet(String key, Object...values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
logger.error("RedisUtils sSet(String key, Object...values) failure."+e.getMessage());
return 0;
}
}
public static long sSetAndTime(String key,long time,Object...values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if(time>0) {
expire(key, time);
}
return count;
} catch (Exception e) {
logger.error("RedisUtils sSetAndTime(String key,long time,Object...values) failure."+e.getMessage());
return 0;
}
}
public static long sGetSetSize(String key){
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
logger.error("RedisUtils sGetSetSize(String key) failure."+e.getMessage());
return 0;
}
}
public static long setRemove(String key, Object ...values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
logger.error("RedisUtils setRemove(String key, Object ...values) failure."+e.getMessage());
return 0;
}
}
public static List<Object> lGet(String key, long start, long end){
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
logger.error("RedisUtils lGet(String key, long start, long end) failure."+e.getMessage());
return null;
}
}
public static long lGetListSize(String key){
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
logger.error("RedisUtils lGetListSize(String key) failure."+e.getMessage());
return 0;
}
}
public static Object lGetIndex(String key,long index){
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
logger.error("RedisUtils lGetIndex(String key,long index) failure."+e.getMessage());
return null;
}
}
public static boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
logger.error("RedisUtils lSet(String key, Object value) failure."+e.getMessage());
return false;
}
}
public static boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
logger.error("RedisUtils lSet(String key, Object value, long time) failure."+e.getMessage());
return false;
}
}
public static boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
logger.error("RedisUtils lSet(String key, List<Object> value) failure."+e.getMessage());
return false;
}
}
public static boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
logger.error("RedisUtils lSet(String key, List<Object> value, long time) failure."+e.getMessage());
return false;
}
}
public static boolean lUpdateIndex(String key, long index,Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
logger.error("RedisUtils lUpdateIndex(String key, long index,Object value) failure."+e.getMessage());
return false;
}
}
public static long lRemove(String key,long count,Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
logger.error("RedisUtils lRemove(String key,long count,Object value) failure."+e.getMessage());
return 0;
}
}
}
获取Bean的工具类
package com.jy.chat_status_server.common.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationHelper implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationHelper.applicationContext= applicationContext;
}
public static Object getBean(String name){
System.out.println(applicationContext);
return applicationContext.getBean(name);
}
}