package com.ohayou.liteshop.cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author liyan
* @date 2020/7/16 下午10:05
*/
@Component
public class RedisService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @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;
}
}
public Set<String> getAll() {
Set<String> keys = redisTemplate.keys("*");
return keys;
}
public void deleteAll() {
Set<String> keys = redisTemplate.keys("*");
redisTemplate.delete(keys);
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
@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=============================
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
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;
}
}
/**
* 递增
*
* @param key 键
* @param delta 要增加几(大于0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
// ================================Map=================================
/**
* HashGet
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return 值
*/
public Object hget(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 获取hashKey对应的所有键值
*
* @param key 键
* @return 对应的多个键值
*/
public Map<Object, Object> hmget(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashSet
*
* @param key 键
* @param map 对应多个键值
* @return true 成功 false 失败
*/
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 并设置时间
*
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
* @return true成功 false失败
*/
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;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @return true 成功 false失败
*/
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;
}
}
/**
* 向一张hash表中放入数据,如果不存在将创建
*
* @param key 键
* @param item 项
* @param value 值
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
* @return true 成功 false失败
*/
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;
}
}
/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param item 项 可以使多个 不能为null
*/
public void hdel(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为null
* @param item 项 不能为null
* @return true 存在 false不存在
*/
public boolean hHasKey(String key, String item) {
return redisTemplate.opsForHash().hasKey(key, item);
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key 键
* @param item 项
* @param by 要增加几(大于0)
* @return
*/
public double hincr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, by);
}
/**
* hash递减
*
* @param key 键
* @param item 项
* @param by 要减少记(小于0)
* @return
*/
public double hdecr(String key, String item, double by) {
return redisTemplate.opsForHash().increment(key, item, -by
没有合适的资源?快使用搜索试试~ 我知道了~
基于springboot + vue前后端分离的完整小型电商系统lite_shop.zip
共491个文件
class:191个
java:191个
xml:94个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 30 浏览量
2024-05-14
13:48:15
上传
评论
收藏 420KB ZIP 举报
温馨提示
该项目利用了基于springboot + vue + mysql的开发模式框架实现的课设系统,包括了项目的源码资源、sql文件、相关指引文档等等。 【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。 【技术】 Java、Python、Node.js、Spring Boot、Django、Express、MySQL、PostgreSQL、MongoDB、React、Angular、Vue、Bootstrap、Material-UI、Redis、Docker、Kubernetes
资源推荐
资源详情
资源评论
收起资源包目录
基于springboot + vue前后端分离的完整小型电商系统lite_shop.zip (491个子文件)
RedisService.class 11KB
AdminUserServiceImpl.class 9KB
MallOrder.class 8KB
AdminMenuServiceImpl.class 8KB
WebLogAspect.class 7KB
SecurityConfig.class 7KB
MallGoodsSpu.class 6KB
JWTTokenUtil.class 6KB
AdminUserDetailsServiceImpl.class 6KB
MallCoupon.class 5KB
GenerateCode.class 5KB
Result.class 4KB
MallAftersale.class 4KB
MallOrderGoods.class 4KB
MallCartItem.class 4KB
MemUser.class 4KB
MallCategory.class 4KB
JwtAuthenticationTokenFilter.class 4KB
MallTopic.class 4KB
MallGoodsComment.class 4KB
AdminMenu.class 4KB
AdminResourceServiceImpl.class 4KB
AdminUser.class 4KB
MallGoodsSku.class 3KB
MemAddress.class 3KB
AdminLog.class 3KB
MallUserCoupon.class 3KB
WebLog.class 3KB
RBACService.class 3KB
AdminUserDetails.class 3KB
SysOrderConfig.class 3KB
RedisConfig.class 3KB
MemOpinion.class 3KB
AdminResource.class 3KB
MallBrand.class 3KB
MallGoodsSpec.class 3KB
AdminRole.class 3KB
MallCouponType.class 3KB
PageQuery.class 3KB
SysFreightConfig.class 3KB
MallGoodsSpecValue.class 3KB
MallCart.class 3KB
MallBrandCategoryRelation.class 3KB
MallCategoryGoodsRelation.class 3KB
AdminRoleResourceRelation.class 3KB
AdminResourceCategory.class 3KB
MallAttrValue.class 3KB
MallAttrGroupRelation.class 3KB
AdminUserVo.class 3KB
MallGoodsAttr.class 3KB
MallBrandGoodsRelation.class 3KB
AdminRoleMenuRelatioin.class 3KB
AdminUserRoleRelation.class 3KB
MallTopicGoods.class 3KB
MemCollect.class 3KB
MemHistory.class 3KB
ErrorCodeMsg.class 2KB
PageUtils.class 2KB
MallAttrGroup.class 2KB
AdminMenuVo.class 2KB
AdminUserController.class 2KB
MyAuthenticationEntryPoint.class 2KB
ExceptionHandler.class 2KB
MyAccessDeniedHandler.class 2KB
AdminRoleServiceImpl.class 1KB
GlobalException.class 1KB
UnAuthenticationException.class 1KB
MyMetaObjectHandler.class 1KB
BaseKey.class 1KB
PermissionDeniedException.class 925B
MyBatisConfig.class 879B
MallCategoryGoodsRelationServiceImpl.class 852B
MallBrandCategoryRelationServiceImpl.class 852B
AdminRoleResourceRelationServiceImpl.class 852B
MallBrandGoodsRelationServiceImpl.class 831B
AdminRoleMenuRelatioinServiceImpl.class 831B
MallAttrGroupRelationServiceImpl.class 824B
AdminResourceCategoryServiceImpl.class 824B
AdminUserRoleRelationServiceImpl.class 824B
MallGoodsSpecValueServiceImpl.class 803B
SysFreightConfigServiceImpl.class 789B
MallGoodsCommentServiceImpl.class 789B
SysOrderConfigServiceImpl.class 775B
MallCouponTypeServiceImpl.class 775B
MallTopicGoodsServiceImpl.class 775B
MallOrderGoodsServiceImpl.class 775B
MallUserCouponServiceImpl.class 775B
MallAftersaleServiceImpl.class 768B
MallGoodsSpecServiceImpl.class 768B
MallAttrGroupServiceImpl.class 768B
MallAttrValueServiceImpl.class 768B
MallGoodsAttrServiceImpl.class 768B
AdminResourceService.class 767B
MallGoodsSpuServiceImpl.class 761B
MallCategoryServiceImpl.class 761B
MallCartItemServiceImpl.class 761B
MallGoodsSkuServiceImpl.class 761B
MemCollectServiceImpl.class 747B
MemAddressServiceImpl.class 747B
MemHistoryServiceImpl.class 747B
共 491 条
- 1
- 2
- 3
- 4
- 5
资源评论
枫蜜柚子茶
- 粉丝: 7398
- 资源: 5103
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功