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的客户 ...
随机推荐
- 【ES】学习6-多字段搜索1
本系列的笔记都来自:https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/multi-field-search.html 下 ...
- php中foreach()跳出循环或者终止循环的实现方法
$arr = array('a','b','c','d','e'); $html = ''; foreach($arr as $key => $value){ if($value=='b'){ ...
- python 全栈开发,Day134(爬虫系列之第1章-requests模块)
一.爬虫系列之第1章-requests模块 爬虫简介 概述 近年来,随着网络应用的逐渐扩展和深入,如何高效的获取网上数据成为了无数公司和个人的追求,在大数据时代,谁掌握了更多的数据,谁就可以获得更高的 ...
- python 全栈开发,Day43(引子,协程介绍,Greenlet模块,Gevent模块,Gevent之同步与异步)
昨日内容回顾 I/O模型,面试会问到I/O操作,不占用CPU.它内部有一个专门的处理I/O模块.print和写log 属于I/O操作,它不占用CPU 线程GIL保证一个进程中的多个线程在同一时刻只有一 ...
- Android使用帧动画内存溢出解决方法
Android使用帧动画内存溢出解决方法https://blog.csdn.net/daitu_liang/article/details/52336015https://blog.csdn.net/ ...
- hdu 1596 乘积的最大值
一般题是 和的最小值 这个是乘积的最大值 Sample Input31 0.5 0.50.5 1 0.40.5 0.4 131 2 //起点 终点2 31 3 Sample Output0.5000. ...
- JDK的get请求方式
package com.example.wang.testapp3; import android.app.ProgressDialog; import android.os.Bundle; impo ...
- 使用Ztree新增角色和编辑角色回显
最近在项目中使用到了ztree,在回显时候费了点时间,特记录下来供下次参考. 1.新增角色使用ztree加载权限,由于权限不多,所以使用直接全部加载. 效果图: 具体涉及ztree代码: jsp中导入 ...
- Cpu 主频与睿频
主频就是一颗CPU的运行频率.比如一颗CPU是2.3G,无论是单核还是多核,所有的核心都是工作在2.3G. 睿频是Intel的一项加速技术,指当启动一个运行程序后,处理器会自动加速到合适的频率,而原来 ...
- poj 3009 冰球 【DFS】求最小步数
题目链接:https://vjudge.net/problem/POJ-3009 转载于:https://www.cnblogs.com/Ash-ly/p/5728439.html 题目大意: 要求把 ...