前言: 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";
- }
- }
来源: http://www.bubuko.com/infodetail-2735209.html