文章目錄
1、配置redis的環境
在虛擬機或者服務器上,安裝redis之後,配置redis的啓動文件
# 需要修改以下內容
# 允許遠程訪問
bind 0.0.0.0
# redis在後臺運行
daemonize yes
# 爲了安全還是設置個密碼吧
requirepass root
配置完成之後啓動 redis 服務 (配置文件路徑需要改成你自己的文件路徑)
./redis-server /usr/local/redis/redis.conf
2、創建SpringBoot項目
2.1、配置依賴
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.2、配置application.properties
# 配置redis ip(改成你自己服務器的ip)
spring.redis.host=39.105.232.73
# redis端口
spring.redis.port=6379
# redis密碼
spring.redis.password=zhelishimima
# 最大連接數
spring.redis.jedis.pool.max-active=10
# 線程池中最大的空閒連接數
spring.redis.jedis.pool.max-idle=8
# 最小空閒連接數
spring.redis.jedis.pool.min-idle=1
# 連接池最大阻塞時間 -1 代表沒有限制
spring.redis.jedis.pool.max-wait=-1
2.3、書寫controller
@RestController
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("getValueByKey")
public String getValueByKey(String key) {
String value = userService.getValueByKey(key);
return value;
}
@RequestMapping("addKeyAndValue")
public String addKeyAndValue(String key, String value) {
try {
userService.addKeyAndValue(key, value);
return "添加成功";
} catch (Exception e) {
return "添加失敗";
}
}
}
2.4、書寫service
public interface IUserService {
/**
* 根據key獲取對應的value
*/
String getValueByKey(String key);
/**
* 設置一個key和值
*/
void addKeyAndValue(String key, String value);
}
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private RedisManager redisManager;
@Override
public String getValueByKey(String key) {
return redisManager.getValueByKey(key);
}
@Override
public void addKeyAndValue(String key, String value) {
redisManager.addKeyAndValue(key, value);
}
}
2.5、書寫manager
@Component
public class RedisManager {
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 根據key獲取對應的value
*/
public String getValueByKey(String key) {
String value = stringRedisTemplate.opsForValue().get(key);
return value;
}
/**
* 設置一個鍵值對
*/
public void addKeyAndValue(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
}
2.6、測試
分別訪問下列路徑,正常顯示證明redis連接成功
http://127.0.0.1:8080/addKeyAndValue?key=qaz&value=123asd
http://127.0.0.1:8080/getValueByKey?key=qaz
3、常見的API
/**
* 常用的API
* key相關
* 設置key過期的API
* stringRedisTemplate.expire("key", 60, TimeUnit.SECONDS);
* ttl :獲取一個鍵的過期時間
* stringRedisTemplate.getExpire("key");
* 第二個參數表示:返回時間的類型,這裏表示 秒
* stringRedisTemplate.getExpire("key", TimeUnit.SECONDS);
* exists :判斷一個鍵是否存在
* stringRedisTemplate.hasKey("key");
* del :刪除一個鍵值對
* stringRedisTemplate.delete("key");
* randomkey :隨機返回一個key
* stringRedisTemplate.randomKey()
*
* string類型
* set : 設置值,也可以設置4個參數,過期時間,時間類型
* stringRedisTemplate.opsForValue().set("key", "value");
* get : 獲取值
* stringRedisTemplate.opsForValue().get("key");
* mset : 批量設置值
* stringRedisTemplate.opsForValue().multiSet(map<String, String>);
* mget : 批量獲取值
* stringRedisTemplate.opsForValue().multiGet(collection<?>);
* incrby :自增
* stringRedisTemplate.opsForValue().increment("key",1);
* decrby :自減
* stringRedisTemplate.opsForValue().increment("key",-1);
*
* hash類型
* hset :設置一個鍵值對
* stringRedisTemplate.opsForHash().put("key", "field", "value");
* hmset : 批量設置
* stringRedisTemplate.opsForHash().putAll("key", map<?,?>);
* hget : 獲取值
* stringRedisTemplate.opsForHash().get("key", "field");
* hmget :
* 獲取指定 key 對應的鍵值對集合,返回結果爲 map
* stringRedisTemplate.opsForHash().entries("key");
* 獲取指定 key 中field的集合對應的值,返回結果爲 list
* stringRedisTemplate.opsForHash().multiGet("key", collection<?>);
* hdel:刪除指定hash中 field的值可以傳多個
* stringRedisTemplate.opsForHash().delete("key", "field...");
* hexists :判斷hash結構中這個鍵是否存在
* stringRedisTemplate.opsForHash().hasKey()
* hincrby key field increment
* 可爲 整型 添加或減少數據
* stringRedisTemplate.opsForHash().increment("key", "field", "long");
* 可爲 浮點型 添加或減少數據
* stringRedisTemplate.opsForHash().increment("key", "field", "double");
*
* list類型
* lpop rpop 移除:
* stringRedisTemplate.opsForList().leftPop("key")
* 若沒有可移除的,則線程阻塞,2,3參數表示阻塞時間
* stringRedisTemplate.opsForList().leftPop("key", 60, TimeUnit.SECONDS)
* stringRedisTemplate.opsForList().rightPop("key")
* stringRedisTemplate.opsForList().rightPop(key", 60, TimeUnit.SECONDS)
* lpush rpush 添加:
* leftALl, rightAll方法
* stringRedisTemplate.opsForList().rightPush("key", "value");
* stringRedisTemplate.opsForList().rightPushAll("key", "value...");
* stringRedisTemplate.opsForList().rightPushAll("key", collection<String>);
* stringRedisTemplate.opsForList().leftPush("key", "value");
* stringRedisTemplate.opsForList().leftPush("key", "value...");
* stringRedisTemplate.opsForList().leftPush("key", collection<String>);
* stringRedisTemplate.opsForList().rightPush("key", "value");
* stringRedisTemplate.opsForList().rightPush("key", "value");
* 如果存在集合則添加元素:
* stringRedisTemplate.opsForList().leftPushIfPresent("key", "value")
* stringRedisTemplate.opsForList().rightPushIfPresent("key", "value")
* 移除集合中右邊的元素,同時在左邊加入一個元素; 1:選中的集合;2:添加的值
* stringRedisTemplate.opsForList().rightPopAndLeftPush("key", "value")
* 如果超過等待的時間仍沒有元素則退出
* stringRedisTemplate.opsForList().rightPopAndLeftPush("key", "value", 60, TimeUnit.SECONDS)
*
* set類型
* smembers 獲取 set 集合中所有的值; Set<String>
* stringRedisTemplate.opsForSet().members("key")
* sismember key val 判斷指定集合中是否含有指定的值
* stringRedisTemplate.opsForSet().isMember("key", "value")
* sadd 向指定集合設置值,可設置多個
* stringRedisTemplate.opsForSet().add("key", "value...")
* srem key 刪除集合中一個或多個指定的元素
* stringRedisTemplate.opsForSet().remove("key", "value...")
* spop key 刪除並返回集合中的一個隨機元素
* stringRedisTemplate.opsForSet().pop("key")
* spop key [count] 隨機刪除指定個數個元素,返回 list
* stringRedisTemplate.opsForSet().pop("key", count)
* sdiff k1 k2 : 返回指定兩個集合中的差集
* stringRedisTemplate.opsForSet().difference("key1", "key2")
* sinter k1 k2 : 交集
* stringRedisTemplate.opsForSet().intersect("key1", "key2")
* sunion k1 k2 : 並集
* stringRedisTemplate.opsForSet().union("key1", "key2")
*/
4、運行時可能出現的錯誤
io.lettuce.core.RedisCommandExecutionException: ERR invalid password
密碼錯誤