springboot集成redis

第一步引入需要的jar:

compile("org.springframework.boot:spring-boot-starter-data-redis")

第二步骤:

新建一个类

packagecom.zys.util;

importorg.springframework.cache.CacheManager;

importorg.springframework.cache.annotation.EnableCaching;

importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.Configuration;

importorg.springframework.data.redis.cache.RedisCacheManager;

importorg.springframework.data.redis.connection.RedisConnectionFactory;

importorg.springframework.data.redis.core.RedisTemplate;

importorg.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

importorg.springframework.data.redis.serializer.RedisSerializer;

importorg.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration

@EnableCaching

publicclassRedisCacheConfig{

@Bean

publicCacheManagercacheManager(RedisTemplate<?,?>redisTemplate){

CacheManagercacheManager=newRedisCacheManager(redisTemplate);

returncacheManager;

}

@Bean

publicRedisTemplate<String,String>redisTemplate(RedisConnectionFactoryfactory){

RedisTemplate<String,String>redisTemplate=newRedisTemplate<String,String>();

redisTemplate.setConnectionFactory(factory);

//key序列化方式;(不然会出现乱码;),但是如果方法上有Long等非String类型的话,会报类型转换错误;

//所以在没有自己定义key生成策略的时候,以下这个代码建议不要这么写,可以不配置或者自己实现ObjectRedisSerializer

//或者JdkSerializationRedisSerializer序列化方式;

RedisSerializer<String>redisSerializer=newStringRedisSerializer();//Long类型不可以会出现异常信息;

redisTemplate.setKeySerializer(redisSerializer);

redisTemplate.setHashKeySerializer(redisSerializer);

//设置序列化Value的实例化对象

redisTemplate.setValueSerializer(newGenericJackson2JsonRedisSerializer());

returnredisTemplate;

}

}

第三步:

写一个公共类

packagecom.zys.util;

importjava.io.Serializable;

importjava.util.Map;

importjava.util.Set;

importjava.util.concurrent.TimeUnit;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.data.redis.core.RedisTemplate;

importorg.springframework.data.redis.core.ValueOperations;

importorg.springframework.data.redis.serializer.StringRedisSerializer;

importorg.springframework.stereotype.Component;

/**

*redicache工具类

*

*/

@Component

publicclassRedisUtil{

@SuppressWarnings("rawtypes")

@Autowired

privateRedisTemplateredisTemplate;

/**

*批量删除对应的value

*

*@paramkeys

*/

publicvoidremove(finalString...keys){

for(Stringkey:keys){

remove(key);

}

}

/**

*批量删除key

*

*@parampattern

*/

publicvoidremovePattern(finalStringpattern){

Set<Serializable>keys=redisTemplate.keys(pattern);

if(keys.size()>0)

redisTemplate.delete(keys);

}

/**

*删除对应的value

*

*@paramkey

*/

publicvoidremove(finalStringkey){

if(exists(key)){

redisTemplate.delete(key);

}

}

/**

*判断缓存中是否有对应的value

*

*@paramkey

*@return

*/

publicbooleanexists(finalStringkey){

returnredisTemplate.hasKey(key);

}

/**

*读取缓存

*

*@paramkey

*@return

*/

publicStringget(finalStringkey){

Objectresult=null;

redisTemplate.setValueSerializer(newStringRedisSerializer());

ValueOperations<Serializable,Object>operations=redisTemplate.opsForValue();

result=operations.get(key);

if(result==null){

returnnull;

}

returnresult.toString();

}

/**

*写入缓存

*

*@paramkey

*@paramvalue

*@return

*/

publicbooleanset(finalStringkey,Objectvalue){

booleanresult=false;

try{

ValueOperations<Serializable,Object>operations=redisTemplate.opsForValue();

operations.set(key,value);

result=true;

}catch(Exceptione){

e.printStackTrace();

}

returnresult;

}

/**

*写入缓存

*

*@paramkey

*@paramvalue

*@return

*/

publicbooleanset(finalStringkey,Objectvalue,LongexpireTime){

booleanresult=false;

try{

ValueOperations<Serializable,Object>operations=redisTemplate.opsForValue();

operations.set(key,value);

redisTemplate.expire(key,expireTime,TimeUnit.SECONDS);

result=true;

}catch(Exceptione){

e.printStackTrace();

}

returnresult;

}

publicbooleanhmset(Stringkey,Map<String,String>value){

booleanresult=false;

try{

redisTemplate.opsForHash().putAll(key,value);

result=true;

}catch(Exceptione){

e.printStackTrace();

}

returnresult;

}

publicMap<String,String>hmget(Stringkey){

Map<String,String>result=null;

try{

result=redisTemplate.opsForHash().entries(key);

}catch(Exceptione){

e.printStackTrace();

}

returnresult;

}

}

第四步:

在Controller测试即可

@Autowired

privateRedisUtilredisUtil;

redisUtil.set("zys","zys你好,我是redis");

System.out.println(redisUtil.get("zys")+"--------------------------");

相关推荐