1. 程式人生 > 實用技巧 >4.Springboot整合Redis

4.Springboot整合Redis

1.引入依賴

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

2.在yml配置(為了分清格式 所以mysql也貼了出來 以及 我本地沒有設定密碼 所以不需要配置 需要的自行補上)

spring:
  datasource:                                           # 資料來源的相關配置
    url: jdbc:mysql:
//localhost:3306/foodie-shop-dev?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true redis: database: 0 host: localhost port: 6379

3.新增redis工具(懶人必做 基本操作都有,切記需要在啟動類上新增掃描包)

@ComponentScan(basePackages= "com.imooc") //我是放在common公共包 的com.imooc目錄下的

注意:如果新增@ComponentScan報錯,沒有關係,不影響啟動 或者 配置IDEA 不警告(報錯原因主要是因為@SpringBootApplication註解下面已經有@ComponentScan這個註解了。

@Component
public class RedisOperator {


// @Autowired
//    private RedisTemplate<String, Object> redisTemplate;


    @Autowired
    private StringRedisTemplate redisTemplate;


    // Key(鍵),簡單的key-value操作


    /**
     * 實現命令:TTL key,以秒為單位,返回給定 key的剩餘生存時間(TTL, time to live)。
     *
     * @param
key * @return */ public long ttl(String key) { return redisTemplate.getExpire(key); } /** * 實現命令:expire 設定過期時間,單位秒 * * @param key * @return */ public void expire(String key, long timeout) { redisTemplate.expire(key, timeout, TimeUnit.SECONDS); } /** * 實現命令:INCR key,增加key一次 * * @param key * @return */ public long incr(String key, long delta) { return redisTemplate.opsForValue().increment(key, delta); } /** * 實現命令:KEYS pattern,查詢所有符合給定模式 pattern的 key */ public Set<String> keys(String pattern) { return redisTemplate.keys(pattern); } /** * 實現命令:DEL key,刪除一個key * * @param key */ public void del(String key) { redisTemplate.delete(key); } // String(字串) /** * 實現命令:SET key value,設定一個key-value(將字串值 value關聯到 key) * * @param key * @param value */ public void set(String key, String value) { redisTemplate.opsForValue().set(key, value); } /** * 實現命令:SET key value EX seconds,設定key-value和超時時間(秒) * * @param key * @param value * @param timeout * (以秒為單位) */ public void set(String key, String value, long timeout) { redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS); } /** * 實現命令:GET key,返回 key所關聯的字串值。 * * @param key * @return value */ public String get(String key) { return (String)redisTemplate.opsForValue().get(key); } /** * 批量查詢,對應mget * @param keys * @return */ public List<String> mget(List<String> keys) { return redisTemplate.opsForValue().multiGet(keys); } /** * 批量查詢,管道pipeline * @param keys * @return */ public List<Object> batchGet(List<String> keys) { // nginx -> keepalive // redis -> pipeline List<Object> result = redisTemplate.executePipelined(new RedisCallback<String>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { StringRedisConnection src = (StringRedisConnection)connection; for (String k : keys) { src.get(k); } return null; } }); return result; } // Hash(雜湊表) /** * 實現命令:HSET key field value,將雜湊表 key中的域 field的值設為 value * * @param key * @param field * @param value */ public void hset(String key, String field, Object value) { redisTemplate.opsForHash().put(key, field, value); } /** * 實現命令:HGET key field,返回雜湊表 key中給定域 field的值 * * @param key * @param field * @return */ public String hget(String key, String field) { return (String) redisTemplate.opsForHash().get(key, field); } /** * 實現命令:HDEL key field [field ...],刪除雜湊表 key 中的一個或多個指定域,不存在的域將被忽略。 * * @param key * @param fields */ public void hdel(String key, Object... fields) { redisTemplate.opsForHash().delete(key, fields); } /** * 實現命令:HGETALL key,返回雜湊表 key中,所有的域和值。 * * @param key * @return */ public Map<Object, Object> hgetall(String key) { return redisTemplate.opsForHash().entries(key); } // List(列表) /** * 實現命令:LPUSH key value,將一個值 value插入到列表 key的表頭 * * @param key * @param value * @return 執行 LPUSH命令後,列表的長度。 */ public long lpush(String key, String value) { return redisTemplate.opsForList().leftPush(key, value); } /** * 實現命令:LPOP key,移除並返回列表 key的頭元素。 * * @param key * @return 列表key的頭元素。 */ public String lpop(String key) { return (String)redisTemplate.opsForList().leftPop(key); } /** * 實現命令:RPUSH key value,將一個值 value插入到列表 key的表尾(最右邊)。 * * @param key * @param value * @return 執行 LPUSH命令後,列表的長度。 */ public long rpush(String key, String value) { return redisTemplate.opsForList().rightPush(key, value); } }
4.使用 先注入
@Autowired
private RedisOperator redisOperator;

然後直接使用redisOperator即可