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/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="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.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处理,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值对应对象的剩余生命周期

原文地址:https://www.cnblogs.com/wxjnew/p/9161730.html

时间: 2024-08-28 21:56:55

Redis在SSM项目中的简单使用的相关文章

Jwt在Java项目中的简单实际应用

1.什么是jwt 双方之间传递安全信息的简洁的.URL安全的表述性声明规范.JWT作为一个开放的标准(RFC 7519),定义了一种简洁的,自包含的方法用于通信双方之间以Json对象的形式安全的传递信息.简洁(Compact): 可以通过URL,POST参数或者在HTTP header发送,因为数据量小,传输速度也很快 自包含(Self-contained):负载中包含了所有用户所需要的信息,避免了多次查询数据库. 2.Jwt在javaweb项目中的简单使用 第一步:引入maven依赖 <!--

redis缓存在项目中的使用

关于redis为什么能作为缓存这个问题我们就不说了,直接来说一下redis缓存到底如何在项目中使用吧: 1.redis缓存如何在项目中配置? 1.1redis缓存单机版和集群版配置?(redis的客户端jedis常用) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:co

Web项目中创建简单的错误处理页面

当应用程序出现错误的时候,如果没有做错误页面处理的话,会直接输出一些敏感的信息出来,有时候甚至会直接将项目所在的物理路径给显示出来,严重缺乏安全性,并且错误种类繁多,页面风格不一,导致用户体验不好,本文介绍如何在web项目中创建错误页面,从而友好的提示错误信息. 一,创建错误页面error.aspx,前台代码如下(可根据实际需要增加元素): <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=&qu

ActiveMQ的在实际项目中的简单使用

简介 本文达到的目的是会用. 安装服务器: 官网下载win版的,也可是linux版的: 启动服务器:win版的执行activemq.bat 看看是否安装成功:http://localhost:8161/ activeMQ是一个消息服务器, 2中模式:点对点.广播 点对点:消息进入队列后,只会被接收一次,接收完了就在消息队列里消失了. 广播:采用的是广播订阅的方式,所有的订阅了此主题的,当此主题有消息时,都会接收到 可靠性: mq会将信息存储 /* * 消息的类型(消息体): * TextMess

ssm项目中常用的上传文件

在项目中,上传文件一般是必不可少的,所以今天学到新的上传方式,就干脆将学习过的上传方式记录一下 一.表单直接上传图片 表单头要设置 <form action="" method="post" enctype="multipart/form-data"> 元素 <input type="file" name="dwfile" > 后端代码 @RequestMapping(value=&

Android图片缓存技术!直接用在你的项目中,简单方便高效

好久没有写博客了,最近比较繁琐,什么面试呀,找工作呀,大三的生活就快完了,准确的是大学的生活就快完了!三年了,一直在搞移动开发,感觉好快呀,不想就这样的离开学校了!咳咳咳,扯远了... 前不久搞的Android图片缓存,刚开始引入开源的框架,用着还行,但是在开发中遇到问题,就比如universal-image-loader-1.9.5.jar这个框架吧,在加载图片的时候自定义imageview无法加载,可能是存在以下问题吧,况且导入框架导致开发的项目包越来越大,基于上面的这几种情况,于是我就想自

Redis在Php项目中的实际应用场景

商品维度计数 对商品喜欢数,评论数,鉴定数,浏览数进行计数说起电商,肯定离不开商品,而附带商品有各种计数(喜欢数,评论数,鉴定数,浏览数,etc)Redis的命令都是原子性的,你可以轻松地利用INCR,DECR等命令来计数. 采用Redis 的类型: Hash. 如果你对redis数据类型不太熟悉,可以参考http://redis.io/topics/data-types-intro 为product定义个key product:,为每种数值定义hashkey, 譬如喜欢数like_num $r

shiro权限项目中的简单应用

SpringMVC+maven 项目需要使用shiro,所以自学了几天,仅提供给新手,请根据文档查看-该项目仅是测试项目,并不完善,只实现了需要使用的基本功能,并且只提供了使用shiro模块的代码.楼主新人第一次写,如有问题希望能提出来,由衷的感谢. 首先是pom.xml: <dependency>     <groupId>org.apache.shiro</groupId>     <artifactId>shiro-core</artifactI

Linux中ifreq 结构体分析和使用 及其在项目中的简单应用

[基础知识说明] 结构原型: /* * Interface request structure used for socket * ioctl's.  All interface ioctl's must have parameter * definitions which begin with ifr_name.  The * remainder may be interface specific. */ struct ifreq {#define IFHWADDRLEN 6 union {