Spring Boot 菜鳥教程 22 Redis

GitHub

src="//ghbtns.com/github-btn.html?user=je-ge&repo=spring-boot&type=watch&count=true" scrolling="0" width="110" height="20">

簡介

Redis是一個開源的使用ANSI C語言編寫、支持網絡、可基於內存亦可持久化的日誌型、Key-Value數據庫,並提供多種語言的API。

存儲類型

和Memcached類似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set –有序集合)和hash(哈希類型)。這些數據類型都支持push/pop、add/remove及取交集並集和差集及更豐富的操作,而且這些操作都是原子性的。

數據追加方式

在此基礎上,redis支持各種不同方式的排序。與Memcached一樣,爲了保證效率,數據都是緩存在內存中。區別的是redis會週期性的把更新的數據寫入磁盤或者把修改操作寫入追加的記錄文件,並且在此基礎上實現了master-slave(主從)同步。

添加jar包依賴集成Redis

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

RedisService常規操作

package com.jege.spring.boot.service;

import java.io.Serializable;
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.stereotype.Service;

/**
 * @author JE哥
 * @email [email protected]
 * @description:常規操作
 */
@Service
public class RedisService {
  @Autowired
  private RedisTemplate redisTemplate;

  // 批量刪除對應的value
  public void deleteAll(String... keys) {
    for (String key : keys) {
      delete(key);
    }
  }

  // 批量刪除key
  public void deletePattern(String pattern) {
    Set<Serializable> keys = redisTemplate.keys(pattern);
    if (keys.size() > 0)
      redisTemplate.delete(keys);
  }

  // 刪除指定key的value
  public void delete(String key) {
    if (exists(key)) {
      redisTemplate.delete(key);
    }
  }

  // 判斷緩存中是否有對應的value
  public boolean exists(String key) {
    return redisTemplate.hasKey(key);
  }

  // 讀取緩存
  public Object get(String key) {
    ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
    return operations.get(key);
  }

  // 寫入緩存
  public boolean set(String key, Object value) {
    boolean flag = false;
    try {
      ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
      operations.set(key, value);
      flag = true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return flag;
  }

  // 寫入緩存
  public boolean set(String key, Object value, Long expireTime) {
    boolean flag = false;
    try {
      ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
      operations.set(key, value);
      redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
      flag = true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return flag;
  }
}

StringRedisService

package com.jege.spring.boot.service;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @author JE哥
 * @email [email protected]
 * @description:直接操作String數據類型
 */
@Service
public class StringRedisService {
  @Autowired
  public StringRedisTemplate stringRedisTemplate;

  // 獲取某個key的剩餘過期時間
  public long residualExpirationTime(String key) {
    return stringRedisTemplate.getExpire(key);
  }

  // 當key不存在時,爲key賦值
  public boolean setValue(String key, String value) {
    ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
    return ops.setIfAbsent(key, value);
  }

  // 爲key賦值,同時設置過期時間
  public void set(String key, String value, long time) {
    BoundValueOperations<String, String> ops = stringRedisTemplate.boundValueOps(key);
    ops.set(value, time, TimeUnit.SECONDS);
  }

  // 刪除某個key
  public void delete(String key) {
    stringRedisTemplate.delete(key);
  }

  // 判斷某個key是否存在
  public boolean exist(String key) {
    return stringRedisTemplate.hasKey(key);
  }

  // 同redis命令的leftpush
  public void leftPush(String key, String value) {
    stringRedisTemplate.boundListOps(key).leftPush(value);
  }

  // 同redis命令的rightpop
  public String rightPop(String key) {
    return stringRedisTemplate.boundListOps(key).rightPop();
  }
}

application.properties

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-idle=100 
spring.redis.pool.min-idle=1
spring.redis.pool.max-active=1000
spring.redis.pool.max-wait=-1

其他關聯項目

源碼地址

https://github.com/je-ge/spring-boot

如果覺得我的文章或者代碼對您有幫助,可以請我喝杯咖啡。
您的支持將鼓勵我繼續創作!謝謝!
微信打賞
支付寶打賞

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章