我们目前的系统已经实现了广告后台管理和广告前台展示,但是对于首页每天有大量的人访问,对数据库造成很大的访问压力,甚至是瘫痪。那如何解决呢?我们通常的做法有两种:一种是数据缓存、一种是网页静态化。
redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写。企业开发通常采用Redis来实现缓存。同类的产品还有memcache 、memcached 、MongoDB等
Jedis是Redis官方推出的一款面向Java的客户端,提供了很多接口供Java语言调用。可以在Redis官网下载,当然还有一些开源爱好者提供的客户端,如Jredis、SRP等等,推荐使用Jedis
Spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
spring-data-redis针对jedis提供了如下功能:
1. 连接池自动管理,提供了一个高度封装的“RedisTemplate”类
2. 针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
ValueOperations:简单K-V操作
SetOperations:set类型数据操作
ZSetOperations:zset类型数据操作
HashOperations:针对map类型的数据操作
ListOperations:针对list类型的数据操作
<properties>
<spring.version>4.2.4.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jmsartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-context-supportartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.9version>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.8.1version>
dependency>
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>1.7.2.RELEASEversion>
dependency>
dependencies>
# Redis settings
# server IP
# 主机地址
redis.host=127.0.0.1
# server port
# 端口
redis.port=6379
# server pass
redis.pass=
# use dbIndex
redis.database=0
# \u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B
redis.maxIdle=300
# \u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B
redis.maxWait=3000
# \u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684
redis.testOnBorrow=true
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:property-placeholder location="classpath*:properties/*.properties" />
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
bean>
<bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
bean>
beans>
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//使用key/value的形式进行存/取/删除
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class test {
@Autowired
private RedisTemplate redisTemplate;
//使用key/value的形式进行存
@Test
public void setValue(){
redisTemplate.boundValueOps("name").set("曲秃");
}
//使用key/value的形式进行取
@Test
public void getValue(){
String str = (String) redisTemplate.boundValueOps("name").get();
System.out.println(str);
}
//使用key/value的形式进行删除
@Test
public void deleValue(){
redisTemplate.delete("name");
}
}
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//使用Set集合的形式进行存/取/删除
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class settest {
@Autowired
private RedisTemplate redisTemplate;
//使用Set集合的形式进行存
@Test
public void setValue(){
redisTemplate.boundSetOps("name1").add("曲秃");
redisTemplate.boundSetOps("name1").add("张渣");
redisTemplate.boundSetOps("name1").add("段黑");
}
//使用Set集合的形式进行取
@Test
public void getValue(){
Set set = redisTemplate.boundSetOps("name1").members();
System.out.println(set);
}
//使用Set集合的形式进行删除set里数据
@Test
public void removeValue(){
redisTemplate.boundSetOps("name1").remove("张渣");
}
//使用Set集合的形式进行删除set
@Test
public void dele(){
redisTemplate.delete("name1");
}
}
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//使用List的形式进行存/取/删除
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class listtest {
@Autowired
private RedisTemplate redisTemplate;
/**
* 右压栈:先进先出
*/
@Test
public void value1list(){
redisTemplate.boundListOps("list1").rightPush("张渣");
redisTemplate.boundListOps("list1").rightPush("曲秃");
redisTemplate.boundListOps("list1").rightPush("段黑");
}
/**
* 右压栈:查询
*/
@Test
public void getvalue1list(){
List list = redisTemplate.boundListOps("list1").range(0, 10);
System.out.println(list);
}
/**
* 左压栈:后进先出
*/
@Test
public void listvalue2(){
redisTemplate.boundListOps("list2").leftPush("张渣");
redisTemplate.boundListOps("list2").leftPush("曲秃");
redisTemplate.boundListOps("list2").leftPush("段黑");
}
/**
* 左压栈:查询
*/
@Test
public void getlistvalue1(){
//range(0,10);表示查询下标0-10的元素,list集合没有提供查询全部的方法,如果要查询全部,就把后面的值写的特别大
List list = redisTemplate.boundListOps("list2").range(0, 10);
System.out.println(list);
}
/**
* 根据下标查询
*/
@Test
public void searchByIndex(){
String str = (String)redisTemplate.boundListOps("list2").index(1);
System.out.println(str);
}
/**
* 删除值
*/
@Test
public void remove(){
//remove表示删除一个名为张渣的值,如果是remove(2, "张渣")表示删除两个名为张渣的值
redisTemplate.boundListOps("list2").remove(1, "张渣");
}
/**
* 删除集合
*/
@Test
public void dele(){
redisTemplate.delete("list1");
}
}
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@SuppressWarnings("unchecked")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class hashtest {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存值
*/
@Test
public void testSetValue(){
redisTemplate.boundHashOps("namehash").put("a", "曲秃");
redisTemplate.boundHashOps("namehash").put("b", "段黑");
redisTemplate.boundHashOps("namehash").put("c", "张渣");
redisTemplate.boundHashOps("namehash").put("d", "奥特曼");
}
/**
* 获取所有的key
*/
@Test
public void getKeys(){
Set keys = redisTemplate.boundHashOps("namehash").keys();
System.out.println(keys);
}
/**
* 获取所有的value
*/
@Test
public void getValues(){
List values = redisTemplate.boundHashOps("namehash").values();
System.out.println(values);
}
/**
* 根据key删除值
*/
@Test
public void removeByKey(){
redisTemplate.boundHashOps("namehash").delete("c");
}
/**
* 根据key查找值
*/
@Test
public void seachByKey(){
String str = (String) redisTemplate.boundHashOps("namehash").get("c");
System.out.println(str);
}
/**
* 删除全部
*/
@Test
public void dele(){
redisTemplate.delete("namehash");
}
}