博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring下redis的配置
阅读量:6991 次
发布时间:2019-06-27

本文共 5524 字,大约阅读时间需要 18 分钟。

这个项目用到redis,所以学了一下怎样在Spring框架下配置redis。

1、首先是在web.xml中添加Spring的配置文件。

common design
webAppRootKey
webapp.root
contextConfigLocation
classpath:applicationContext.xml,classpath*:mybatis-config.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc-servlet.xml
springmvc
/*

2、然后是redis的配置文件(redis-config.xml)文件。

在Spring的配置文件中引用redis的配置文件

3、新建redis.properties,里面包含redis连接需要的配置信息

#redis setting  redis.host=127.0.0.1redis.port=6379redis.password=123456redis.maxIdle=100redis.maxActive=300redis.maxWait=1000redis.testOnBorrow=trueredis.timeout=100000fep.local.cache.capacity =10000

一定要注意,每行后面千万不要有空格,我就是因为这个问题卡了一两个小时= =

4、编写RedisUtil.java,里面放有redis的增删改查操作。

package com.test.fep.redis;import java.io.Serializable;  import java.util.Set;  import java.util.concurrent.TimeUnit;  import org.springframework.data.redis.core.RedisTemplate;  import org.springframework.data.redis.core.ValueOperations;  public class RedisUtil {    private RedisTemplate
redisTemplate; /** * 批量删除对应的value * * @param keys */ public void remove(final String... keys) { for (String key : keys) { remove(key); } } /** * 批量删除key * * @param pattern */ public void removePattern(final String pattern) { Set
keys = redisTemplate.keys(pattern); if (keys.size() > 0) redisTemplate.delete(keys); } /** * 删除对应的value * * @param key */ public void remove(final String key) { if (exists(key)) { redisTemplate.delete(key); } } /** * 判断缓存中是否有对应的value * * @param key * @return */ public boolean exists(final String key) { return redisTemplate.hasKey(key); } /** * 读取缓存 * * @param key * @return */ public Object get(final String key) { Object result = null; ValueOperations
operations = redisTemplate.opsForValue(); result = operations.get(key); return result; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations
operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { logger.error("set cache error", e); } return result; } /** * 写入缓存 * * @param key * @param value * @return */ public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations
operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { logger.error("set cache error", e); } return result; } public long increment(final String key , long delta){ return redisTemplate.opsForValue().increment(key, delta); } public void setRedisTemplate(RedisTemplate
redisTemplate) { this.redisTemplate = redisTemplate; }}

5、在功能代码中调用RedisUtil类中的方法,

package com.test.fep.service.impl;import java.math.BigDecimal;import java.util.Date;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.test.fep.domain.SysAppLoginToken;import com.test.fep.mapper.SysAppLoginTokenMapper;import com.test.fep.redis.RedisUtil;import com.test.fep.service.AuthService;import net.sf.json.JSONObject;@Service("authService")public class AuthServiceImpl implements AuthService{    @Autowired    private SysAppLoginTokenMapper sysAppLoginTokenMapper ;    @Autowired    private RedisUtil redisUtil;  //记得注入        @Override    public SysAppLoginToken verification(String tokenId) {        SysAppLoginToken token = null;        if (redisUtil.exists(tokenId)) {            token = (SysAppLoginToken) redisUtil.get(tokenId);  //从缓存中查找token        }else{            token = sysAppLoginTokenMapper.selectByPrimaryKey(tokenId) ;            redisUtil.set(tokenId, token);    //将token写入缓存         }                return null;    }}

好了,到这里就在一个项目中完整地使用了redis。

还需要注意的一点是,所有保存在Redis中的接口都必须要实现Serializable接口

转载于:https://www.cnblogs.com/zhaoyan001/p/7515650.html

你可能感兴趣的文章
solrnet - document
查看>>
第十一节: 封装通用的定时调度框架,实现新增、删除、开启、暂停计划任务:...
查看>>
checkbox阻止事件
查看>>
关于HTTP协议学习(二)
查看>>
web集群时session同步的3种方法
查看>>
(转)asp.net 高质量缩略图
查看>>
【面经】阿里学长小谈面试
查看>>
进程和线程—Python多线程编程
查看>>
【原创】使用 PKG 包安装 MYSQL( SunOS)
查看>>
python常用模块
查看>>
C#属性有什么作用
查看>>
163. 不同的二叉查找树
查看>>
Batsing的网页编程规范(HTML/CSS/JS/PHP)
查看>>
vue 1.x 总结
查看>>
关于Android Launcher图标上面动态改变数字的实现
查看>>
android设备不识别awk命令,缺少busybox
查看>>
利用接口“组装电脑”
查看>>
LeetCode – Refresh – Plus One
查看>>
LeetCode 90: Subsets II
查看>>
2017-2018-1 20155222 《信息安全系统设计基础》第十四周学习总结
查看>>