当前位置:网站首页>Redis tool class redisutil (tool class III)
Redis tool class redisutil (tool class III)
2022-07-07 01:56:00 【Novice Zhang~】
package com.menglar.soap.item.common.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
@Component
public class RedisUtil {
@Resource
private RedisTemplate<String, Object> redisTemplate;
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
//=============================common============================
/** * Specify cache expiration time * * @param key key * @param time Time ( second ) * @return */
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * according to key Get expiration time * * @param key key Not for null * @return Time ( second ) return 0 Stands for permanent validity */
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/** * Judge key Whether there is * * @param key key * @return true There is false non-existent */
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * Delete cache * * @param key You can pass a value Or more */
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
//============================String=============================
/** * Normal cache fetch * * @param key key * @return value */
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/** * Normal cache put in * * @param key key * @param value value * @return true success false Failure */
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * The normal cache is put in and set the time * * @param key key * @param value value * @param time Time ( second ) time Be greater than 0 If time Less than or equal to 0 Will be set indefinitely * @return true success false Failure */
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean setByDay(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.DAYS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * Increasing * * @param key key * @param delta How much more should I add ( Greater than 0) * @return */
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException(" The increment factor must be greater than 0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/** * Decline * * @param key key * @param delta To cut it down a few ( Less than 0) * @return */
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException(" The decline factor must be greater than 0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
//================================Map=================================
/** * HashGet * * @param key key Not for null * @param item term Not for null * @return value */
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/** * obtain hashKey All the corresponding key values * * @param key key * @return Corresponding multiple key values */
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/** * HashSet * * @param key key * @param map Corresponding to multiple key values * @return true success false Failure */
public boolean hmset(String key, Map<String, Object> map) {
try {
redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * HashSet And set the time * * @param key key * @param map Corresponding to multiple key values * @param time Time ( second ) * @return true success false Failure */
public boolean hmset(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * To a piece of hash Put data in the table , If it doesn't exist, it will create * * @param key key * @param item term * @param value value * @return true success false Failure */
public boolean hset(String key, String item, Object value) {
try {
redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * To a piece of hash Put data in the table , If it doesn't exist, it will create * * @param key key * @param item term * @param value value * @param time Time ( second ) Be careful : If there is already hash Watch has time , This will replace the original time * @return true success false Failure */
public boolean hset(String key, String item, Object value, long time) {
try {
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * Delete hash The values in the table * * @param key key Not for null * @param item term Can make multiple Not for null */
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/** * Judge hash Whether there is a value of this item in the table * * @param key key Not for null * @param item term Not for null * @return true There is false non-existent */
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/** * hash Increasing If it doesn't exist , It creates a And return the added value to * * @param key key * @param item term * @param by How much more should I add ( Greater than 0) * @return */
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/** * hash Decline * * @param key key * @param item term * @param by Remember less ( Less than 0) * @return */
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by);
}
//============================set=============================
/** * according to key obtain Set All the values in * * @param key key * @return */
public Set<Object> sGet(String key) {
try {
return redisTemplate.opsForSet().members(key);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/** * according to value From a set Query in , Whether there is * * @param key key * @param value value * @return true There is false non-existent */
public boolean sHasKey(String key, Object value) {
try {
return redisTemplate.opsForSet().isMember(key, value);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * Put data into set cache * * @param key key * @param values value It can be more than one * @return The number of successes */
public long sSet(String key, Object... values) {
try {
return redisTemplate.opsForSet().add(key, values);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/** * take set Data is put into the cache * * @param key key * @param time Time ( second ) * @param values value It can be more than one * @return The number of successes */
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = redisTemplate.opsForSet().add(key, values);
if (time > 0) {
expire(key, time);
}
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/** * obtain set The length of the cache * * @param key key * @return */
public long sGetSetSize(String key) {
try {
return redisTemplate.opsForSet().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/** * The removal value is value Of * * @param key key * @param values value It can be more than one * @return Number of removed */
public long setRemove(String key, Object... values) {
try {
Long count = redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
//===============================list=================================
/** * obtain list The contents of the cache * * @param key key * @param start Start * @param end end 0 To -1 For all values * @return */
public List<Object> lGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/** * obtain list The length of the cache * * @param key key * @return */
public long lGetListSize(String key) {
try {
return redisTemplate.opsForList().size(key);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/** * Through the index obtain list The value in * * @param key key * @param index Indexes index>=0 when , 0 Header ,1 The second element , By analogy ;index<0 when ,-1, Tail ,-2 The next to last element , By analogy * @return */
public Object lGetIndex(String key, long index) {
try {
return redisTemplate.opsForList().index(key, index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/** * take list Put into cache * * @param key key * @param value value * @return */
public boolean lSet(String key, Object value) {
try {
redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * take list Put into cache * * @param key key * @param value value * @param time Time () * @return */
public boolean lSet(String key, Object value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) {
redisTemplate.expire(key,time,TimeUnit.DAYS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * take list Put into cache * * @param key key * @param value value * @return */
public boolean lSet(String key, List<Object> value) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * take list Put into cache * * @param key key * @param value value * @param time Time ( second ) * @return */
public boolean lSet(String key, List<Object> value, long time) {
try {
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * Modify according to the index list A piece of data in * * @param key key * @param index Indexes * @param value value * @return */
public boolean lUpdateIndex(String key, long index, Object value) {
try {
redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/** * remove N The values are value * * @param key key * @param count How many removed * @param value value * @return Number of removed */
public long lRemove(String key, long count, Object value) {
try {
Long remove = redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/** * Use gzip Compress string * * @param originString The string to compress * @return Compressed string */
public String compress(String originString) {
if (originString == null || originString.length() == 0) {
return originString;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (
GZIPOutputStream gzip = new GZIPOutputStream(out);
) {
gzip.write(originString.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return new sun.misc.BASE64Encoder().encode(out.toByteArray());
}
/** * Use gzip decompression * * @param compressedString Compress string * @return */
public String uncompress(String compressedString) {
if (compressedString == null || compressedString.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] compressedByte = new byte[0];
try {
compressedByte = new sun.misc.BASE64Decoder().decodeBuffer(compressedString);
} catch (Exception e) {
e.printStackTrace();
}
String originString = null;
try (
ByteArrayInputStream in = new ByteArrayInputStream(compressedByte);
GZIPInputStream ginzip = new GZIPInputStream(in);
) {
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = ginzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
originString = out.toString();
} catch (IOException e) {
e.printStackTrace();
}
return originString;
}
}
边栏推荐
- Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
- npm install 编译时报“Cannot read properties of null (reading ‘pickAlgorithm‘)“
- C language [23] classic interview questions [Part 2]
- IDEA常用的快捷键
- How did partydao turn a tweet into a $200million product Dao in one year
- 场景实践:基于函数计算快速搭建Wordpress博客系统
- ROS learning (25) rviz plugin
- AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
- AcWing 1140. 最短网络 (最小生成树)
- Input and output of C language pointer to two-dimensional array
猜你喜欢
ROS learning (XIX) robot slam function package cartographer
centos8安裝mysql報錯:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins
shell脚本快速统计项目代码行数
爬虫实战(六):爬笔趣阁小说
永久的摇篮
js如何快速创建一个长度为 n 的数组
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
Scenario practice: quickly build wordpress blog system based on function calculation
刨析《C语言》【进阶】付费知识【完结】
AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
随机推荐
刨析《C语言》【进阶】付费知识【完结】
Compile command line terminal swift
AcWing 344. Solution to the problem of sightseeing tour (Floyd finding the minimum ring of undirected graph)
Analyze "C language" [advanced] paid knowledge [II]
ROS学习(26)动态参数配置
mongodb查看表是否导入成功
ROS learning (XIX) robot slam function package cartographer
JS ES5也可以创建常量?
ZOJ problem set – 2563 long dominoes [e.g. pressure DP]
C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式
猫猫回收站
HDU 4661 message passing (wood DP & amp; Combinatorics)
设置Wordpress伪静态连接(无宝塔)
使用nodejs完成判断哪些项目打包+发版
IDEA常用的快捷键
Appium自动化测试基础 — uiautomatorviewer定位工具
LeetCode. 剑指offer 62. 圆圈中最后剩下的数
centos8安裝mysql報錯:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins
ROS learning (21) robot slam function package -- installation and testing of orbslam
字符串转成日期对象