springboot配置redis(單節點)

springboot配置redis

1、引入 spring-boot-starter-redis(1.4版本前),spring-boot-starter-data-redis(1.4版本後)

<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-redis</artifactId>  
</dependency>

2,添加配置文件

複製代碼
# REDIS (RedisProperties)
# Redis數據庫索引(默認爲0)
spring.redis.database=0  
# Redis服務器地址
spring.redis.host=192.168.0.58
# Redis服務器連接端口
spring.redis.port=6379  
# Redis服務器連接密碼(默認爲空)
spring.redis.password=  
# 連接池最大連接數(使用負值表示沒有限制)
spring.redis.pool.max-active=8  
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.pool.max-wait=-1  
# 連接池中的最大空閒連接
spring.redis.pool.max-idle=8  
# 連接池中的最小空閒連接
spring.redis.pool.min-idle=0  
# 連接超時時間(毫秒)
spring.redis.timeout=0
複製代碼

3.Redis緩存配置類提供redisTemplate(獲得配置文件中連接參數後的)

複製代碼
@Configuration
@EnableCaching
public class RedisCacheConfig {
    @Bean
    public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate){
        CacheManager cacheManager = new RedisCacheManager(redisTemplate);
        return cacheManager;
    }
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String,String>();
        redisTemplate.setConnectionFactory(factory);
        // key序列化方式;(不然會出現亂碼;),但是如果方法上有Long等非String類型的話,會報類型轉換錯誤;
        // 所以在沒有自己定義key生成策略的時候,以下這個代碼建議不要這麼寫,可以不配置或者自己實現ObjectRedisSerializer
        // 或者JdkSerializationRedisSerializer序列化方式;
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();// Long類型不可以會出現異常信息;
        redisTemplate.setKeySerializer(redisSerializer);
        redisTemplate.setHashKeySerializer(redisSerializer);
        return redisTemplate;
    }
    
}
複製代碼

4,Redis工具類

複製代碼
package com.ty.tyzxtj.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 工具類
 * 
 */
@SuppressWarnings("unchecked")
@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<Serializable> 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<Serializable, Object> 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<Serializable, Object> 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<Serializable, Object> 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<String, String> value) {
        boolean result = false;
        try {
            redisTemplate.opsForHash().putAll(key, value);
            result = true;
        } catch (Exception e) {
        e.printStackTrace();
        }
        return result;
    }
    
    public  Map<String,String> hmget(String key) {
        Map<String,String> result =null;
        try {
            result=  redisTemplate.opsForHash().entries(key);
        } catch (Exception e) {
        e.printStackTrace();
        }
        return result;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章