Redis在SSM项目中的简单使用
一、基于SSM的Redis环境配置
前提是你的开发电脑安装和配置好了redis,如果没安装请看Window配置Redis环境和简单使用
1.1、pom文件中引入redis客户端jar包(pom.xml)
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.</version>
</dependency>
1.2、redis属性配置文件(redis.properties)
#redis.host=127.0.0.1
redis.host=localhost
redis.port=
redis.password=你的redis密码
redis.maxIdle=
redis.maxTotal=
redis.maxWaitMillis=
redis.testOnBorrow=true
redis.timeout=
1.3、spring和redis的配置文件(spring-redis.xml)
指定了redis属性配置文件的路径
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd"> <util:properties id="redisConfig" location="classpath:/config/redis.properties"></util:properties> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="#{redisConfig['redis.maxIdle']}" />
<property name="maxTotal" value="#{redisConfig['redis.maxTotal']}" />
<property name="maxWaitMillis" value="#{redisConfig['redis.maxWaitMillis']}" />
<property name="testOnBorrow" value="#{redisConfig['redis.testOnBorrow']}" />
</bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg index="" ref="jedisPoolConfig" />
<!-- 端口,默认6379 -->
<constructor-arg index="" value="#{redisConfig['redis.host']}" name="host" type="java.lang.String"/>
<constructor-arg index="" value="#{redisConfig['redis.port']}" name="port" type="int"/>
<constructor-arg index="" value="#{redisConfig['redis.timeout']}" name="timeout" type="int"/>
<constructor-arg index="" value="#{redisConfig['redis.password']}" name="password" type="java.lang.String"/>
</bean> </beans>
1.4、springmvc中引入Spring和redis的配置(spring-mvc.xml)
最下方利用import标签引入redis的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 注解扫描包 -->
<context:component-scan base-package="com.king.weixin"/>
<!-- 开启注解 -->
<mvc:annotation-driven/>
<!--
配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,.04新增功能,需要重新设置spring-mvc-3.0.xsd
-->
<mvc:resources mapping="/img/**" location="/img/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/html/**" location="/html/" />
<mvc:resources mapping="/tinymce/**" location="/tinymce/" />
<mvc:resources mapping="/upload/**" location="/upload/" />
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- redis配置 -->
<import resource="spring-redis.xml"/>
</beans>
二、测试和验证
采用jedis获取redis资源和操作redis,添加值并且给值设置生命周期
public String addStringValue(String key, String value, int expireSeconds) {
String result = null;
Jedis jedis = null;
try {
jedis = jedisManager.getResource();
//result = jedis.set(key, value);
result = jedis.set(key,value).toString();
if (expireSeconds != ) {
//EXPIRE key seconds 为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。
jedis.expire(key, expireSeconds);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jedisManager.returnResource(jedis);
}
return result;
}
获取值得生命周期方法,ttl (key)
public long getStringValueTTLByKey(String key){
long result = ;
Jedis jedis = null;
try {
jedis = jedisManager.getResource();
//Redis TTL 命令以秒为单位返回 key 的剩余过期时间。
result = jedis.ttl(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedisManager.returnResource(jedis);
}
return result;
}
在命令行查看redis中的所有key值和剩余生命周期,如下图可以使用keys * 查看所有缓存的key ,利用TTL key可以查看该key值对应对象的剩余生命周期

Redis在SSM项目中的简单使用的更多相关文章
- redis在java项目中的使用
在上一篇文章中已经讲了redis的spring配置,这篇将会描述redis在java项目中的使用. redis存储形式都是key-value(键值对),按照存储的内容分为两种,一种是存简单数据,即数字 ...
- Jwt在Java项目中的简单实际应用
1.什么是jwt 双方之间传递安全信息的简洁的.URL安全的表述性声明规范.JWT作为一个开放的标准(RFC 7519),定义了一种简洁的,自包含的方法用于通信双方之间以Json对象的形式安全的传递信 ...
- ssm项目中遇到微信用户名称带有表情,插入失败问题
ssm项目中遇到微信用户名称带有表情,插入失败问题 问题 Mysql的utf8编码最多3个字节,而Emoji表情或者某些特殊字符是4个字节. 因此会导致带有表情的昵称插入数据库时出错. 解决方法 一. ...
- Redis在新项目中的使用场景
Redis在新项目中的使用场景 数据类型 使用场景 string 比如说,我想知道什么时候封锁一个Ip地址,Incrby命令(使用这个命令记录被访问的次数) Hash 存储用户的信息[id,name, ...
- ssm项目中中文字符乱码
昨天给同学改一个错,在SSM项目中,表单输入的String类型中,中文字符值,总是乱码,在控制器层获取的数据就开始乱码,先后进行了如下排查: web.xml中配置设置字符编码的监听器(过滤器), js ...
- 当你的SSM项目中的springmvc.xml发生第一行错误解决方案
当你新建了一个SSM项目,你复制网上的xml文件来配置或者你下载了一个SSM项目打开发现xml文件错误,打开是第一行报错的时候你是不是很懵逼 或者是这样 总之就是xml文件中<?xml vers ...
- python JoinableQueue在生产者消费者项目中的简单应用
class multiprocessing.JoinableQueue([maxsize]) JoinableQueue, a Queue subclass, is a queue which add ...
- Redis在实际项目中的一应用场景
1.在游戏的等级排名,可以将用户信息放入到redis的有序集合中,然后取得相应的排名,不用自己写代码去排序. 2.利用rediss的数据特性的自增,自减属性,可以将项目中的一些列入阅读数,点赞数放入到 ...
- redis缓存在项目中的使用
关于redis为什么能作为缓存这个问题我们就不说了,直接来说一下redis缓存到底如何在项目中使用吧: 1.redis缓存如何在项目中配置? 1.1redis缓存单机版和集群版配置?(redis的客户 ...
随机推荐
- 性能测试三十九:Jprofiler分析CPU过高和响应时间长的问题
使用Jprofiler监控分析案例 一.cpu负载过高:http://localhost:8080/PerfTeach/CpuTopServlet?id=1 cpu消耗高的可能原因1.使用了复杂的算法 ...
- python 全栈开发,Day72(昨日作业讲解,昨日内容回顾,Django多表创建)
昨日作业讲解 1.图书管理系统 实现功能:book单表的增删改查 1.1 新建一个项目bms,创建应用book.过程略... 1.2 手动创建static目录,并在目录里面创建css文件夹,修改set ...
- python 全栈开发,Day48(标准文档流,块级元素和行内元素,浮动,margin的用法,文本属性和字体属性)
昨日内容回顾 高级选择器: 后代选择 : div p 子代选择器 : div>p 并集选择器: div,p 交集选择器: div.active 属性选择器: [属性~='属性值'] 伪类选择器 ...
- HDU2873 Bomb Game(二维SG函数)
Bomb Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- POJ 1742 Coins 【多重背包DP】
题意:有n种面额的硬币.面额.个数分别为A_i.C_i,求最多能搭配出几种不超过m的金额? 思路:dp[j]就是总数为j的价值是否已经有了这种方法,如果现在没有,那么我们就一个个硬币去尝试直到有,这种 ...
- BZOJ2287【POJ Challenge】消失之物
题解: 1.以前见过类似的,可以cdq分治 当l=r时就是还有一个剩余 这样时间是nmlogn的 空间是mlogn 2.首先我们可以dp出表示出j的方案数 令g[i][j]表示不能选i,表示出j的方案 ...
- C#对Windows文件/文件夹/目录的一些操作总结
1. 在一个目录下创建一个文件夹 if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path); ...
- Codeforces Round #441(Div.2) F - High Cry
F - High Cry 题目大意:给你n个数,让你找区间里面所有数或 起来大于区间里面最大数的区间个数. 思路:反向思维,找出不符合的区间然后用总数减去.我们找出每个数掌控的最左端 和最右端,一个数 ...
- 6-2 铁轨 uva 514
较为简单的stack题目 但是还是犯了一些错误: 1. 要想清空栈,直接重新定义较为方便! 2.在if(s.top()==x)时 加上 !s.empty() 否则程序会崩溃 3. 必须要加上i- ...
- 练习|Django-单表
结构目录 页面展示: 1创建Django,创建app01 在modules.py添加 class Book(models.Model): id=models.AutoField(primary_key ...