前言:redis使用的非常广泛,其优点是速度快、支持丰富的数据类型、支持事物操作等,适用于会话缓存(session cache)、全页缓存(FPC)、队列等,极大的减少了数据库的负担。
1.安装下载redis
网址:https://github.com/MicrosoftArchive/redis/releases,安装成功后,开启redis服务。
2.导入jar包
commons-pool.jar jedis.jar spring-data-redis.jar fastjson.jar aopalliance.jar
注意:jedis和Commons-pool两个jar包的版本是有对应关系的,注意引用的时候要配对使用,否则将报错。
3.配置redis文件
redis.properties:
redis.hostName=127.0.0.1
redis.port=6379
redis.timeout=15000
redis.usePool=true
redis.maxTotal=32
redis.maxIdle=20
redis.minIdle=10
redis.minEvictableIdleTimeMillis=300000
redis.numTestsPerEvictionRun=3
redis.timeBetweenEvictionRunsMillis=60000
spring-redis.xml:
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
<!-- redis 单点配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"></property>
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="minIdle" value="${redis.minIdle}"></property>
<property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"></property>
<property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"></property>
<property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"></property>
<property name="testOnBorrow" value="true"></property>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
<property name="database" value="5"></property>
<property name="poolConfig" ref="jedisPoolConfig"></property>
<property name="hostName" value="${redis.hostName}"></property>
<property name="port" value="${redis.port}"></property>
<property name="timeout" value="${redis.timeout}"></property>
<property name="usePool" value="true"></property>
</bean>
<bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"></property>
</bean>
<bean id="redisUtil" class="com.util.RedisUtil">
<property name="redisTemplate" ref="jedisTemplate"></property>
</bean>
4.在spring的主配置文件中填入以下信息:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>
<import resource="spring-redis.xml" />
5.创建工具类(RedisUtil)
package com.util;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
/**
*
* <p>Title: Redis操作类</p>
* <p>Description: redis操作的基本方法</p>
* <p>Company: yangcnye</p>
*/
public class RedisUtil<T> {
private final static String redisCode = "utf-8";
protected RedisTemplate<String, T> redisTemplate;
public RedisTemplate<String, T> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<String, T> redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 字符串添加
* @param key
* @param value
* @param liveTime
*/
public void set(String key, String value, long liveTime) {
try {
this.set(key.getBytes(redisCode), value.getBytes(redisCode), liveTime);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
5.测试缓存是否成功,创建个controller
package com.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.alibaba.fastjson.JSON;
import com.util.RedisUtil;
/***
* redis缓存运用
*
* @author yangcnye
*
*/
@Controller
@RequestMapping("/redis")
public class RedisController {
private static int i = 1;
private static int j = 1;
private static int k = 0;
private static final int l = 0;
@Autowired
private RedisUtil<Object> redis;
@RequestMapping(value = "/RedisTest", method = RequestMethod.POST)
public String RedisTest(@RequestParam("description") String description) {
System.out.println(description);
redis.set("num", JSON.toJSONString(description), 6000);
return "success";
}
}
以上就是基于spring项目中的redis简单应用。