什么是工具类 java_java 工具类使用

importjava.util.List;importjava.util.Map;importjava.util.Set;importjava.util.concurrent.TimeUnit;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.util.CollectionUtils;/*** 基于spring和redis的redisTemplate工具类

* 针对所有的hash 都是以h开头的方法

* 针对所有的Set 都是以s开头的方法 不含通用方法

* 针对所有的List 都是以l开头的方法*/

public classRedisUtil {private RedisTemplateredisTemplate;public void setRedisTemplate(RedisTemplateredisTemplate) {this.redisTemplate =redisTemplate;

}//=============================common============================

/*** 指定缓存失效时间

*@paramkey 键

*@paramtime 时间(秒)

*@return

*/

public boolean expire(String key,longtime){try{if(time>0){

redisTemplate.expire(key, time, TimeUnit.SECONDS);

}return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 根据key 获取过期时间

*@paramkey 键 不能为null

*@return时间(秒) 返回0代表为永久有效*/

public longgetExpire(String key){returnredisTemplate.getExpire(key,TimeUnit.SECONDS);

}/*** 判断key是否存在

*@paramkey 键

*@returntrue 存在 false不存在*/

public booleanhasKey(String key){try{returnredisTemplate.hasKey(key);

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 删除缓存

*@paramkey 可以传一个值 或多个*/@SuppressWarnings("unchecked")public voiddel(String ... key){if(key!=null&&key.length>0){if(key.length==1){

redisTemplate.delete(key[0]);

}else{

redisTemplate.delete(CollectionUtils.arrayToList(key));

}

}

}//============================String=============================

/*** 普通缓存获取

*@paramkey 键

*@return值*/

publicObject get(String key){return key==null?null:redisTemplate.opsForValue().get(key);

}/*** 普通缓存放入

*@paramkey 键

*@paramvalue 值

*@returntrue成功 false失败*/

public booleanset(String key,Object value) {try{

redisTemplate.opsForValue().set(key, value);return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 普通缓存放入并设置时间

*@paramkey 键

*@paramvalue 值

*@paramtime 时间(秒) time要大于0 如果time小于等于0 将设置无限期

*@returntrue成功 false 失败*/

public boolean set(String key,Object value,longtime){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;

}

}/*** 递增

*@paramkey 键

*@paramby 要增加几(大于0)

*@return

*/

public long incr(String key, longdelta){if(delta<0){throw new RuntimeException("递增因子必须大于0");

}returnredisTemplate.opsForValue().increment(key, delta);

}/*** 递减

*@paramkey 键

*@paramby 要减少几(小于0)

*@return

*/

public long decr(String key, longdelta){if(delta<0){throw new RuntimeException("递减因子必须大于0");

}return redisTemplate.opsForValue().increment(key, -delta);

}//================================Map=================================

/*** HashGet

*@paramkey 键 不能为null

*@paramitem 项 不能为null

*@return值*/

publicObject hget(String key,String item){returnredisTemplate.opsForHash().get(key, item);

}/*** 获取hashKey对应的所有键值

*@paramkey 键

*@return对应的多个键值*/

public Maphmget(String key){returnredisTemplate.opsForHash().entries(key);

}/*** HashSet

*@paramkey 键

*@parammap 对应多个键值

*@returntrue 成功 false 失败*/

public boolean hmset(String key, Mapmap){try{

redisTemplate.opsForHash().putAll(key, map);return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** HashSet 并设置时间

*@paramkey 键

*@parammap 对应多个键值

*@paramtime 时间(秒)

*@returntrue成功 false失败*/

public boolean hmset(String key, Map map, longtime){try{

redisTemplate.opsForHash().putAll(key, map);if(time>0){

expire(key, time);

}return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 向一张hash表中放入数据,如果不存在将创建

*@paramkey 键

*@paramitem 项

*@paramvalue 值

*@returntrue 成功 false失败*/

public booleanhset(String key,String item,Object value) {try{

redisTemplate.opsForHash().put(key, item, value);return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 向一张hash表中放入数据,如果不存在将创建

*@paramkey 键

*@paramitem 项

*@paramvalue 值

*@paramtime 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间

*@returntrue 成功 false失败*/

public boolean hset(String key,String item,Object value,longtime) {try{

redisTemplate.opsForHash().put(key, item, value);if(time>0){

expire(key, time);

}return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 删除hash表中的值

*@paramkey 键 不能为null

*@paramitem 项 可以使多个 不能为null*/

public voidhdel(String key, Object... item){

redisTemplate.opsForHash().delete(key,item);

}/*** 判断hash表中是否有该项的值

*@paramkey 键 不能为null

*@paramitem 项 不能为null

*@returntrue 存在 false不存在*/

public booleanhHasKey(String key, String item){returnredisTemplate.opsForHash().hasKey(key, item);

}/*** hash递增 如果不存在,就会创建一个 并把新增后的值返回

*@paramkey 键

*@paramitem 项

*@paramby 要增加几(大于0)

*@return

*/

public double hincr(String key, String item,doubleby){returnredisTemplate.opsForHash().increment(key, item, by);

}/*** hash递减

*@paramkey 键

*@paramitem 项

*@paramby 要减少记(小于0)

*@return

*/

public double hdecr(String key, String item,doubleby){return redisTemplate.opsForHash().increment(key, item,-by);

}//============================set=============================

/*** 根据key获取Set中的所有值

*@paramkey 键

*@return

*/

public SetsGet(String key){try{returnredisTemplate.opsForSet().members(key);

}catch(Exception e) {

e.printStackTrace();return null;

}

}/*** 根据value从一个set中查询,是否存在

*@paramkey 键

*@paramvalue 值

*@returntrue 存在 false不存在*/

public booleansHasKey(String key,Object value){try{returnredisTemplate.opsForSet().isMember(key, value);

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 将数据放入set缓存

*@paramkey 键

*@paramvalues 值 可以是多个

*@return成功个数*/

public longsSet(String key, Object...values) {try{returnredisTemplate.opsForSet().add(key, values);

}catch(Exception e) {

e.printStackTrace();return 0;

}

}/*** 将set数据放入缓存

*@paramkey 键

*@paramtime 时间(秒)

*@paramvalues 值 可以是多个

*@return成功个数*/

public long sSetAndTime(String key,longtime,Object...values) {try{

Long count=redisTemplate.opsForSet().add(key, values);if(time>0) expire(key, time);returncount;

}catch(Exception e) {

e.printStackTrace();return 0;

}

}/*** 获取set缓存的长度

*@paramkey 键

*@return

*/

public longsGetSetSize(String key){try{returnredisTemplate.opsForSet().size(key);

}catch(Exception e) {

e.printStackTrace();return 0;

}

}/*** 移除值为value的

*@paramkey 键

*@paramvalues 值 可以是多个

*@return移除的个数*/

public longsetRemove(String key, Object ...values) {try{

Long count=redisTemplate.opsForSet().remove(key, values);returncount;

}catch(Exception e) {

e.printStackTrace();return 0;

}

}//===============================list=================================

/*** 获取list缓存的内容

*@paramkey 键

*@paramstart 开始

*@paramend 结束 0 到 -1代表所有值

*@return

*/

public List lGet(String key,long start, longend){try{returnredisTemplate.opsForList().range(key, start, end);

}catch(Exception e) {

e.printStackTrace();return null;

}

}/*** 获取list缓存的长度

*@paramkey 键

*@return

*/

public longlGetListSize(String key){try{returnredisTemplate.opsForList().size(key);

}catch(Exception e) {

e.printStackTrace();return 0;

}

}/*** 通过索引 获取list中的值

*@paramkey 键

*@paramindex 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推

*@return

*/

public Object lGetIndex(String key,longindex){try{returnredisTemplate.opsForList().index(key, index);

}catch(Exception e) {

e.printStackTrace();return null;

}

}/*** 将list放入缓存

*@paramkey 键

*@paramvalue 值

*@paramtime 时间(秒)

*@return

*/

public booleanlSet(String key, Object value) {try{

redisTemplate.opsForList().rightPush(key, value);return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 将list放入缓存

*@paramkey 键

*@paramvalue 值

*@paramtime 时间(秒)

*@return

*/

public boolean lSet(String key, Object value, longtime) {try{

redisTemplate.opsForList().rightPush(key, value);if (time > 0) expire(key, time);return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 将list放入缓存

*@paramkey 键

*@paramvalue 值

*@paramtime 时间(秒)

*@return

*/

public boolean lSet(String key, Listvalue) {try{

redisTemplate.opsForList().rightPushAll(key, value);return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 将list放入缓存

*@paramkey 键

*@paramvalue 值

*@paramtime 时间(秒)

*@return

*/

public boolean lSet(String key, List value, longtime) {try{

redisTemplate.opsForList().rightPushAll(key, value);if (time > 0) expire(key, time);return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 根据索引修改list中的某条数据

*@paramkey 键

*@paramindex 索引

*@paramvalue 值

*@return

*/

public boolean lUpdateIndex(String key, longindex,Object value) {try{

redisTemplate.opsForList().set(key, index, value);return true;

}catch(Exception e) {

e.printStackTrace();return false;

}

}/*** 移除N个值为value

*@paramkey 键

*@paramcount 移除多少个

*@paramvalue 值

*@return移除的个数*/

public long lRemove(String key,longcount,Object value) {try{

Long remove=redisTemplate.opsForList().remove(key, count, value);returnremove;

}catch(Exception e) {

e.printStackTrace();return 0;

}

}/**获取集合长度

*@paramkey

*@return

*/

public longgetkeylistsize(String key){returnredisTemplate.opsForList().size(key);

}public longpushlist(String key,String value){returnredisTemplate.opsForList().leftPush(key, value);

}public Setgetkeys(String key){return redisTemplate.keys(key +"*");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值