-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chao
committed
Dec 10, 2021
1 parent
8aba757
commit d2185f2
Showing
6 changed files
with
187 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.example.jwt.service; | ||
|
||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.StringUtils; | ||
|
||
import javax.annotation.Resource; | ||
import java.io.Serializable; | ||
import java.util.Date; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Redis 服务工具类 | ||
* | ||
* @author : Charles | ||
* @date : 2021/12/10 | ||
*/ | ||
@Service | ||
public class RedisService { | ||
@Resource | ||
private RedisTemplate<Serializable, Object> redisTemplate; | ||
/** | ||
* 读取缓存 | ||
*/ | ||
public Object get(String key) { | ||
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); | ||
return operations.get(key); | ||
} | ||
/** | ||
* 判断缓存中是否存在 | ||
*/ | ||
public boolean exists(String key) { | ||
return StringUtils.hasLength(key) && Boolean.TRUE.equals(redisTemplate.hasKey(key)); | ||
} | ||
/** | ||
* 删除缓存 | ||
*/ | ||
public void remove(String key) { | ||
if (exists(key)) { | ||
redisTemplate.delete(key); | ||
} | ||
} | ||
/** | ||
* 写入缓存 | ||
*/ | ||
public boolean set(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; | ||
} | ||
/** | ||
* 写入缓存 并 加上过期时间 | ||
*/ | ||
public boolean set(String key, Object value, Date date) { | ||
boolean result = false; | ||
try { | ||
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); | ||
operations.set(key, value); | ||
redisTemplate.expireAt(key, date); | ||
result = true; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return result; | ||
} | ||
/** | ||
* 写入过期时间(毫秒) | ||
*/ | ||
public boolean expire(String key, Long expireTimeMillis) { | ||
boolean result = false; | ||
try { | ||
redisTemplate.expire(key, expireTimeMillis, TimeUnit.MILLISECONDS); | ||
result = true; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters