redis単純パッケージと使用
8673 ワード
1、準備プロジェクトにjarパッケージを導入し、mavenの方式 プロファイル構成redisのデータソース
2、常用方法の抽象
3、常用方法の実現
4、具体的な使用上記2つのファイルを直接プロジェクトに格納し、メソッドを呼び出すことでredisキャッシュ は、単一のプロジェクトを作成し、上の2つのファイルをプロジェクトに配置し、jarファイルにパッケージ化し、プロジェクトで使用する必要がある場合に導入することができます.これにより、より一般的な 抽象クラスのメソッドは一般的なメソッドであり、具体的な使用シーンに応じてカスタム抽象メソッドを追加し、redisを使用しても他を使用しても、この抽象クラスを実現することができ、具体的な実装は異なる
org.springframework.boot
spring-boot-starter-data-redis
spring.redis.database=1
spring.redis.host=localhost
spring.redis.password=123456
spring.redis.port=6379
2、常用方法の抽象
public interface S2Cache {
/**
*
*
* @return
*/
String getName();
/**
*
*
* @param key
* @return
*/
boolean exists(final String key);
/**
*
*
* @param key
* @return
*/
Object get(final String key);
/**
*
*
* @param key
* @param clazz
* @param
* @return
*/
T get(final String key, Class clazz);
/**
*
*
* @param keys
* @param clazz
* @param
* @return
*/
List multiGet(Collection keys, Class clazz);
/**
*
*
* @param key
* @param clazz
* @param
* @return
*/
List getList(final String key, Class clazz);
/**
*
*
* @param key
* @param value
* @return
*/
void set(final String key, Object value);
/**
*
*
* @param key
* @param value
* @param expireTime
* @return
*/
void set(final String key, Object value, Long expireTime);
/**
*
*
* @param key
*/
void remove(final String key);
/**
*
*
* @param keys
*/
void remove(final String... keys);
/**
*
*
* @param key
* @param value
* @return
*/
long increment(final String key, long value);
/**
*
*
* @param key
* @param value
* @param hashKey
* @return
*/
void hashPush(final String key, String hashKey, Object value);
/**
*
*
* @param key
* @param value
* @param hashKey
* @param expireTime
* @return
*/
void hashPush(final String key, String hashKey, Object value, Long expireTime);
/**
*
*
* @param key
* @param map
* @return
*/
void hashPush(final String key, Map map);
/**
*
*
* @param key
* @param map
* @param expireTime
* @return
*/
void hashPush(final String key, Map map, Long expireTime);
/**
*
*
* @param key
* @param hashKey
* @return
*/
Object hashPop(final String key, String hashKey);
/**
*
*
* @param key
* @param hashKey
* @param clazz
* @param
* @return
*/
T hashPop(final String key, String hashKey, Class clazz);
/**
* hash
*
* @param key
* @param hashKey
* @return
*/
boolean hashHasKey(final String key, String hashKey);
/**
* hash
*
* @param key
* @param hashKey
*/
void hashRemove(final String key, String hashKey);
/**
* key
*
* @param key
* @return
*/
Set
3、常用方法の実現
@Component
public class RedisCache implements S2Cache {
@Value("${sisyphe.s2cache.expireTime:10800}")
public Long expireTime;
@Autowired
private RedisTemplate redisTemplate;
@Override
public String getName() {
return "redis";
}
@Override
public boolean exists(String key) {
return redisTemplate.hasKey(key);
}
@Override
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public T get(String key, Class clazz) {
return JSON.parseObject(JSON.toJSONString(get(key)), clazz);
}
/**
*
*
* @param keys
* @param clazz
* @return
*/
@Override
public List multiGet(Collection keys, Class clazz) {
return (List) redisTemplate.opsForValue().multiGet(keys);
}
/**
*
*
* @param key
* @param clazz
* @return
*/
@Override
public List getList(String key, Class clazz) {
return JSON.parseArray(JSON.toJSONString(get(key)), clazz);
}
@Override
public void set(String key, Object value) {
set(key, value, expireTime);
}
@Override
public void set(String key, Object value, Long expireTime) {
if (expireTime == null) {
redisTemplate.opsForValue().set(key, value);
} else {
redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);
}
}
@Override
public void remove(String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
@Override
public void remove(String... keys) {
for (String key : keys) {
if (exists(key)) {
remove(key);
}
}
}
@Override
public long increment(String key, long value) {
return redisTemplate.opsForValue().increment(key, value);
}
@Override
public void hashPush(String key, String hashKey, Object value) {
hashPush(key, hashKey, value, null);
}
@Override
public void hashPush(String key, String hashKey, Object value, Long expireTime) {
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put(key, hashKey, value);
if (expireTime != null) {
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
}
/**
*
*
* @param key
* @param map
* @return
*/
@Override
public void hashPush(String key, Map map) {
hashPush(key, map, null);
}
/**
*
*
* @param key
* @param map
* @param expireTime
* @return
*/
@Override
public void hashPush(String key, Map map, Long expireTime) {
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.putAll(key, map);
if (expireTime != null) {
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
}
}
@Override
public Object hashPop(String key, String hashKey) {
return redisTemplate.opsForHash().get(key, hashKey);
}
/**
*
*
* @param key
* @param hashKey
* @param clazz
* @return
*/
@Override
public T hashPop(String key, String hashKey, Class clazz) {
return JSON.parseObject(JSON.toJSONString(hashPop(key, hashKey)), clazz);
}
@Override
public boolean hashHasKey(String key, String hashKey) {
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
@Override
public void hashRemove(String key, String hashKey) {
redisTemplate.opsForHash().delete(key, hashKey);
}
@Override
public Set hashKeys(String key) {
return redisTemplate.opsForHash().keys(key);
}
@Override
public List hashValues(String key) {
return redisTemplate.opsForHash().values(key);
}
@Override
public long hashIncrement(String key, String hashKey, long value) {
return redisTemplate.opsForHash().increment(key, hashKey, value);
}
@Override
public Map hashEntries(String hashKey) {
return redisTemplate.opsForHash().entries(hashKey);
}
}
4、具体的な使用