springboot集成redis

阅读更多
第一步引入需要的jar :
compile("org.springframework.boot:spring-boot-starter-data-redis")


第二步骤:
新建一个类
package com.zys.util;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
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.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableCaching
public class RedisCacheConfig {
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate){
        CacheManager cacheManager = new RedisCacheManager(redisTemplate);
        return cacheManager;
    }
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory){
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(factory);
        // key序列化方式;(不然会出现乱码;),但是如果方法上有Long等非String类型的话,会报类型转换错误;
        // 所以在没有自己定义key生成策略的时候,以下这个代码建议不要这么写,可以不配置或者自己实现ObjectRedisSerializer
        // 或者JdkSerializationRedisSerializer序列化方式;
        RedisSerializer redisSerializer = new StringRedisSerializer();// Long类型不可以会出现异常信息;
        redisTemplate.setKeySerializer(redisSerializer);
        redisTemplate.setHashKeySerializer(redisSerializer);
        //设置序列化Value的实例化对象
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }
   
}


第三步:

写一个公共类
package com.zys.util;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
/**
* redicache 工具类
*
*/
@Component
public class RedisUtil {
@SuppressWarnings("rawtypes")
@Autowired
private RedisTemplate redisTemplate;
/**
* 批量删除对应的value
*
* @param keys
*/
public void remove(final String... keys) {
    for (String key : keys) {
    remove(key);
    }
}
/**
* 批量删除key
*
* @param pattern
*/
public void removePattern(final String pattern) {
    Set keys = redisTemplate.keys(pattern);
    if (keys.size() > 0)
    redisTemplate.delete(keys);
}
/**
* 删除对应的value
*
* @param key
*/
public void remove(final String key) {
    if (exists(key)) {
    redisTemplate.delete(key);
    }
}
/**
* 判断缓存中是否有对应的value
*
* @param key
* @return
*/
public boolean exists(final String key) {
    return redisTemplate.hasKey(key);
}
/**
* 读取缓存
*
* @param key
* @return
*/
public String get(final String key) {
    Object result = null;
    redisTemplate.setValueSerializer(new StringRedisSerializer());
    ValueOperations operations = redisTemplate.opsForValue();
    result = operations.get(key);
    if(result==null){
        return null;
    }
    return result.toString();
}
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value) {
    boolean result = false;
    try {
    ValueOperations operations = redisTemplate.opsForValue();
    operations.set(key, value);
    result = true;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return result;
}
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
    boolean result = false;
    try {
    ValueOperations operations = redisTemplate.opsForValue();
    operations.set(key, value);
    redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
    result = true;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return result;
    }

    public  boolean hmset(String key, Map value) {
        boolean result = false;
        try {
            redisTemplate.opsForHash().putAll(key, value);
            result = true;
        } catch (Exception e) {
        e.printStackTrace();
        }
        return result;
    }
   
    public  Map hmget(String key) {
        Map result =null;
        try {
            result=  redisTemplate.opsForHash().entries(key);
        } catch (Exception e) {
        e.printStackTrace();
        }
        return result;
    }
}

第四步:
在Controller  测试即可


@Autowired
private RedisUtil redisUtil;




redisUtil.set("zys", "zys你好,我是redis");
System.out.println(redisUtil.get("zys")+"--------------------------");

你可能感兴趣的:(redis,springboot)