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.0</version></dependency>

1.2、redis属性配置文件(redis.properties)

#redis.host=127.0.0.1
redis.host=localhost
redis.port=6379
redis.password=你的redis密码
redis.maxIdle=50
redis.maxTotal=100
redis.maxWaitMillis=3000
redis.testOnBorrow=true
redis.timeout=5000

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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/utilhttp://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="0" ref="jedisPoolConfig" /><!-- 端口,默认6379 --><constructor-arg index="1" value="#{redisConfig['redis.host']}" name="host" type="java.lang.String"/><constructor-arg index="2" value="#{redisConfig['redis.port']}"  name="port" type="int"/><constructor-arg index="3" value="#{redisConfig['redis.timeout']}"  name="timeout" type="int"/><constructor-arg index="4" 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.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"><!-- 注解扫描包 --><context:component-scan base-package="com.king.weixin"/><!-- 开启注解 --><mvc:annotation-driven/><!--配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.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 != 0) {//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 = 0;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值对应对象的剩余生命周期

posted on 2018-06-09 23:17 kingstudy 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/wxjnew/p/9161730.html

Redis在SSM项目中的简单使用相关推荐

  1. Redis在Web项目中的应用与实践

    Redis作为一个开源的(BSD)基于内存的高性能存储系统,已经被各大互联网公司广泛使用,并且有着诸多的应用场景.本篇文章将基于PHP来详细讲解Redis在Web项目中的主要应用与实践. 缓存 这里所 ...

  2. ssm项目中使用拦截器加上不生效解决方案

    ssm项目中使用拦截器加上不生效解决方案 参考文章: (1)ssm项目中使用拦截器加上不生效解决方案 (2)https://www.cnblogs.com/xiufengchen/p/11205283 ...

  3. SSM项目中怎样引入并使用Bootstrap

    Bootstrap 官网 https://v3.bootcss.com/getting-started/#download 下载 进入官网,点击用于生产环境的Bootstrap下的下载Bootstra ...

  4. SSM项目中整合WebService

    先了解一下WebService的一些相关术语吧: WebService: WebService是一种跨编程语言和跨操作系统平台的远程调用技术. WSDL(web service definition ...

  5. Vue项目中最简单的使用集成UEditor方式,含图片上传

    Vue 3 项目参考这里 前言 封面是UEditor的 百度指数 折线图.虽然今天已经是 2018 年,且优秀的富文本编辑器层出不穷(包括移动端),但从图中可以看出UEditor仍然维持着较高的搜索热 ...

  6. Vue项目中最简单的使用集成百度UEditor方式,含图片上传

    前言 封面是UEditor的 百度指数 折线图.虽然今天已经是 2018 年,且优秀的富文本编辑器层出不穷(包括移动端),但从图中可以看出UEditor仍然维持着较高的搜索热度.而不少公司和个人也仍然 ...

  7. 关于SSM项目中配置文件的一些心得

    SSM项目指的是运用Spring,SpringMVC,Mybatis三大框架的项目,个人理解其中Spring的主要作用是依赖注入,控制反转,依赖注入就是创建bean,保存Bean,控制反转不太理解,希 ...

  8. ssm项目中重定向和转发的区别

    在做ssm项目时会有页面跳转,页面跳转就会有重定向和转发这两种不同的方式,在昨天我做了个ssm构建的增删改查的小项目,对这两种方式有了一定的理解,接下来我会讲一下自己的想法. 转发 转发可以在两个页面 ...

  9. Redis在PHP项目中的应用

    一 运行redis服务端 出现上图的图形,就说明redis服务端开启成功,并且开启了密码功能(如果不加载配置文件,连接redis是不需要密码的,这样,会给我们的程序带来很大隐患) 密码的设置: 在re ...

最新文章

  1. 数据结构与算法 / 跳表
  2. 牛客 - 交换(思维+找循环节)
  3. 《剑指offer》栈的压入、弹出序列
  4. synaptic不停抖动后自动关闭的问题
  5. laravel框架总结(一) -- 请求和响应
  6. 天体运行动图,如此美妙,如此震撼!
  7. FreeBSD9.1安装Gnome2桌面
  8. .net环境下ckeditor与ckfinder中文文件链接乱码的问题
  9. matplotlib plot 分组_Python数据分析模块二:Matplotlib
  10. 杭电oj-----Farm Irrigation(BFS)
  11. css grid随页面大小_前端三大布局,float,flex,grid的介绍。
  12. 开源电子海图和webGIS
  13. centos7下载php7.4
  14. flex java blazeds 注解_flex java blazeds 注解
  15. mac_ScreenSaver_第1个屏幕保护程序
  16. Boom 3D环绕音效软件免费安装使用教程
  17. 单元测试--学习大纲
  18. 计算机对外访问端口受限,打开445端口提示拒绝访问(445端口访问受限原因和解决法)...
  19. 解决win10访问xp服务器共享文件出现smb1问题
  20. Android 华为手机获取相册图片路径,获取不到问题

热门文章

  1. setAnimationTransition:forView:cache: 运行动画时背景色问题
  2. 2015.08.15冒泡排序
  3. MariaDB10和MySQL5.6社区版压力测试
  4. :架构优化在何时,方成为公司的推动力与核心竞争力
  5. DS二叉树--左叶子数量
  6. Android环信爬坑指北(二)头像昵称好友备注显示
  7. vim中设置python代码缩进为4个空格
  8. 远程调用RestTemplate
  9. 网络编程(发送get和post请求到服务器端,并获取响应)
  10. Microsoft-Office-Professional-Plus-2007