From 2fcd97552d16718cc7997629fd637a73a5a4483f Mon Sep 17 00:00:00 2001 From: 郑永安 <zyazyz250@sina.com> Date: 星期一, 19 六月 2023 14:44:19 +0800 Subject: [PATCH] 删除 --- src/main/java/com/gk/firework/Config/Oauth2/IRedisService.java | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 180 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/gk/firework/Config/Oauth2/IRedisService.java b/src/main/java/com/gk/firework/Config/Oauth2/IRedisService.java new file mode 100644 index 0000000..a1dee75 --- /dev/null +++ b/src/main/java/com/gk/firework/Config/Oauth2/IRedisService.java @@ -0,0 +1,180 @@ +package com.gk.firework.Config.Oauth2; + +import com.alibaba.fastjson.JSON; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; +import java.util.concurrent.TimeUnit; + +/** + * redis service + * + * @author zhangby + * @date 2019-05-15 09:34 + */ +@Service +public class IRedisService { + /** + * logger + */ + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + /** + * redis service + */ + @Autowired + protected StringRedisTemplate redisTemplate; + + /** + * Write to redis cache + * + * @param key key + * @param value value + * @return boolean + */ + public boolean set(final String key, Object value) { + boolean result = false; + if (value != null) { + try { + ValueOperations operations = redisTemplate.opsForValue(); + if (value instanceof String) { + operations.set(key, value.toString()); + } else { + operations.set(key, JSON.toJSONString(value)); + } + result = true; + } catch (Exception e) { + logger.info("Writing redis cache failed! The error message is:" + e.getMessage()); + e.printStackTrace(); + } + } + return result; + } + + /** + * Write redis cache (set expire survival time) + * + * @param key key + * @param value value + * @param expire time + * @return boolean + */ + public boolean set(final String key, Object value, Long expire) { + boolean result = false; + try { + ValueOperations operations = redisTemplate.opsForValue(); + if (value instanceof String) { + operations.set(key, value.toString()); + } else { + operations.set(key, JSON.toJSONString(value)); + } + redisTemplate.expire(key, expire, TimeUnit.SECONDS); + result = true; + } catch (Exception e) { + logger.info("Writing to the redis cache (setting the expire lifetime) failed! The error message is:" + e.getMessage()); + e.printStackTrace(); + } + return result; + } + + + /** + * Read redis cache + * + * @param key key + * @return object + */ + public Object get(final String key) { + Object result = null; + try { + ValueOperations operations = redisTemplate.opsForValue(); + result = operations.get(key); + } catch (Exception e) { + logger.info("Failed to read redis cache! The error message is:" + e.getMessage()); + e.printStackTrace(); + } + return result; + } + + /** + * Read redis to entity + * + * @param key redis key + * @param clazz 实体类class + * @param <T> 泛型 + * @return T + */ + public <T> T getBean(final String key, Class<T> clazz) { + return Optional.ofNullable(get(key)) + .map(o -> JSON.parseObject(o.toString(), clazz)) + .orElse(null); + } + + /** + * Read redis to List + * @param key + * @param clazz + * @param <T> + * @return + */ + public <T> List<T> getArrayBean(final String key, Class<T> clazz) { + return Optional.ofNullable(get(key)) + .map(o -> JSON.parseArray(o.toString(), clazz)) + .orElse(null); + } + + + /** + * Determine if there is a corresponding key in the redis cache + * + * @param key key + * @return boolean + */ + public boolean exists(final String key) { + boolean result = false; + try { + result = redisTemplate.hasKey(key); + } catch (Exception e) { + logger.info("Determine if there is a corresponding key in the redis cache failed! The error message is:" + e.getMessage()); + e.printStackTrace(); + } + return result; + } + + /** + * Redis deletes the corresponding value according to the key + * + * @param key key + * @return boolean + */ + public boolean remove(final String key) { + boolean result = false; + try { + if (exists(key)) { + redisTemplate.delete(key); + } + result = true; + } catch (Exception e) { + logger.info("Redis fails to delete the corresponding value according to the key! The error message is:" + e.getMessage()); + e.printStackTrace(); + } + return result; + } + + /** + * Redis deletes the corresponding value according to the keywords batch + * + * @param keys keys + */ + public void remove(final String... keys) { + for (String key : keys) { + remove(key); + } + } +} -- Gitblit v1.9.2