Redis在SSM项目中的简单使用
生活随笔
收集整理的這篇文章主要介紹了
Redis在SSM项目中的简单使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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=50001.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项目中的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Netty 系列一(核心组件和实例).
- 下一篇: 课程学习心得体会