{"id":128,"date":"2021-11-22T12:48:39","date_gmt":"2021-11-22T04:48:39","guid":{"rendered":"https:\/\/www.zhangshuwei.com\/?p=128"},"modified":"2021-11-22T12:48:39","modified_gmt":"2021-11-22T04:48:39","slug":"redis%e5%b0%81%e8%a3%85","status":"publish","type":"post","link":"https:\/\/www.zhangshuwei.com\/index.php\/2021\/11\/22\/redis%e5%b0%81%e8%a3%85\/","title":{"rendered":"Redis\u5c01\u88c5"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">\/*\r\n * Copyright 2016-2021 Pnoker. All Rights Reserved.\r\n * Licensed under the Apache License, Version 2.0 (the \"License\");\r\n * you may not use this file except in compliance with the License.\r\n * You may obtain a copy of the License at\r\n *     http:\/\/www.apache.org\/licenses\/LICENSE-2.0\r\n * Unless required by applicable law or agreed to in writing, software\r\n * distributed under the License is distributed on an \"AS IS\" BASIS,\r\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n * See the License for the specific language governing permissions and\r\n * limitations under the License.\r\n *\/\r\n\r\npackage com.dc3.common.utils;\r\n\r\nimport cn.hutool.core.convert.Convert;\r\nimport lombok.extern.slf4j.Slf4j;\r\nimport org.springframework.data.redis.core.RedisTemplate;\r\nimport org.springframework.stereotype.Component;\r\n\r\nimport javax.annotation.Resource;\r\nimport java.util.Arrays;\r\nimport java.util.List;\r\nimport java.util.concurrent.TimeUnit;\r\n\r\n\/**\r\n * @author pnoker\r\n *\/\r\n@Slf4j\r\n@Component\r\npublic class RedisUtil {\r\n\r\n    @Resource\r\n    private RedisTemplate&lt;String, Object> redisTemplate;\r\n\r\n    \/**\r\n     * \u5224\u65ad Key \u662f\u5426\u5b58\u5728\r\n     *\r\n     * @param key String key\r\n     * @return boolean\r\n     *\/\r\n    public boolean hasKey(String key) {\r\n        try {\r\n            Boolean hasKey = redisTemplate.hasKey(key);\r\n            return null != hasKey ? hasKey : false;\r\n        } catch (Exception e) {\r\n            log.error(e.getMessage(), e);\r\n        }\r\n        return false;\r\n    }\r\n\r\n    \/**\r\n     * \u6dfb\u52a0 Key \u7f13\u5b58\r\n     *\r\n     * @param key   String key\r\n     * @param value Object\r\n     *\/\r\n    public void setKey(String key, Object value) {\r\n        try {\r\n            redisTemplate.opsForValue().set(key, value);\r\n        } catch (Exception e) {\r\n            log.error(e.getMessage(), e);\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * \u6dfb\u52a0 Key \u7f13\u5b58,\u5e76\u8bbe\u7f6e\u5931\u6548\u65f6\u95f4\r\n     *\r\n     * @param key   String key\r\n     * @param value Object\r\n     * @param time  Time\r\n     * @param unit  TimeUnit\r\n     *\/\r\n    public void setKey(String key, Object value, long time, TimeUnit unit) {\r\n        try {\r\n            redisTemplate.opsForValue().set(key, value, time, unit);\r\n        } catch (Exception e) {\r\n            log.error(e.getMessage(), e);\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * \u83b7\u53d6 Key \u7f13\u5b58\r\n     *\r\n     * @param key String key\r\n     * @param &lt;T> T\r\n     * @return T\r\n     *\/\r\n    public &lt;T> T getKey(String key, Class&lt;T> type) {\r\n        try {\r\n            Object object = redisTemplate.opsForValue().get(key);\r\n            return Convert.convert(type, object);\r\n        } catch (Exception e) {\r\n            log.error(e.getMessage(), e);\r\n        }\r\n        return null;\r\n    }\r\n\r\n    \/**\r\n     * \u83b7\u53d6 Keys \u7f13\u5b58\r\n     *\r\n     * @param keys String key array\r\n     * @param &lt;T>  T\r\n     * @return T Array\r\n     *\/\r\n    public &lt;T> List&lt;T> getKeys(List&lt;String> keys, Class&lt;T> type) {\r\n        try {\r\n            List&lt;Object> objects = redisTemplate.opsForValue().multiGet(keys);\r\n            return Convert.toList(type, objects);\r\n        } catch (Exception e) {\r\n            log.error(e.getMessage(), e);\r\n        }\r\n        return null;\r\n    }\r\n\r\n    \/**\r\n     * \u5220\u9664 Keys \u7f13\u5b58\r\n     *\r\n     * @param keys Key Array\r\n     *\/\r\n    public void removeKey(String... keys) {\r\n        if (null != keys &amp;&amp; keys.length > 0) {\r\n            try {\r\n                if (keys.length == 1) {\r\n                    redisTemplate.delete(keys[0]);\r\n                } else {\r\n                    redisTemplate.delete(Arrays.asList(keys));\r\n                }\r\n            } catch (Exception e) {\r\n                log.error(e.getMessage(), e);\r\n            }\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * \u6307\u5b9a\u952e\u503c\u5931\u6548\u65f6\u95f4\r\n     *\r\n     * @param key  String key\r\n     * @param time Time\r\n     * @param unit TimeUnit\r\n     *\/\r\n    public void expire(String key, long time, TimeUnit unit) {\r\n        try {\r\n            if (time > 0) {\r\n                redisTemplate.expire(key, time, unit);\r\n            }\r\n        } catch (Exception e) {\r\n            log.error(e.getMessage(), e);\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * \u83b7\u53d6 Key \u5931\u6548\u65f6\u95f4\r\n     *\r\n     * @param key  String key\r\n     * @param unit TimeUnit\r\n     * @return long\r\n     *\/\r\n    public long expire(String key, TimeUnit unit) {\r\n        try {\r\n            Long expire = redisTemplate.getExpire(key, unit);\r\n            return null != expire ? expire : 0L;\r\n        } catch (Exception e) {\r\n            log.error(e.getMessage(), e);\r\n        }\r\n        return 0L;\r\n    }\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[28,37],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-servers","tag-java","tag-redis"],"_links":{"self":[{"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":1,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":129,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/posts\/128\/revisions\/129"}],"wp:attachment":[{"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangshuwei.com\/index.php\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}