1.pom.xml引入相关依赖
<!-- 缓存 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
2.在src/main/resources下创建properties文件夹,建立redis-config.properties
redis.host=192.168.***.***
redis.port=6379
<!--有密码需要指定 没有不需要-->
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
3.在src/main/resources下创建spring文件夹 ,创建applicationContext-redis.xml
<?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"
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">
<!-- 加载属性文件-->
<context:property-placeholder location="classpath:properties/redis-config.properties"/>
<!-- 配置redis连接池相关属性-->
<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}"/>
<property name="maxTotal" value="${redis.total}"/>
</bean>
<!-- 配置redis连接工厂-->
<bean id="RedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<property name="hostName" value="${redis.host}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.pass}"/>
<property name="database" value="${redis.database}"/>
<!--连接池的属性 -->
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!-- RedisTemplate-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="RedisConnectionFactory"/>
</bean>
</beans>
maxIdle :最大空闲数
maxWaitMillis:连接时的最大等待毫秒数
testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;
4.相关操作
//值类型操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestValue {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void setValue(){
redisTemplate.boundValueOps("name").set("youjiuye");
}
@Test
public void getValue(){
String str = (String) redisTemplate.boundValueOps("name").get();
System.out.println(str);
}
@Test
public void deleteValue(){
redisTemplate.delete("name");;
}
}
//Set类型操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
public class TestSet {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存入值
*/
@Test
public void setValue(){
redisTemplate.boundSetOps("nameset").add("曹操");
redisTemplate.boundSetOps("nameset").add("刘备");
redisTemplate.boundSetOps("nameset").add("孙权");
}
/**
* 提取值
*/
@Test
public void getValue(){
Set members = redisTemplate.boundSetOps("nameset").members();
System.out.println(members);
}
/**
* 删除集合中的某一个值
*/
@Test
public void deleteValue(){
redisTemplate.boundSetOps("nameset").remove("孙权");
}
/**
* 删除整个集合
*/
@Test
public void deleteAllValue(){
redisTemplate.delete("nameset");
}
}
//List类型操作
//创建测试类TestList
//右压栈
/**
* 右压栈:后添加的对象排在后边
*/
@Test
public void testSetValue1(){
redisTemplate.boundListOps("namelist1").rightPush("刘备");
redisTemplate.boundListOps("namelist1").rightPush("关羽");
redisTemplate.boundListOps("namelist1").rightPush("张飞");
}
/**
* 显示右压栈集合
*/
@Test
public void testGetValue1(){
List list = redisTemplate.boundListOps("namelist1").range(0, 10);
System.out.println(list);
}
//运行结果:
//[刘备, 关羽, 张飞]
//左压栈
/**
* 左压栈:后添加的对象排在前边
*/
@Test
public void testSetValue2(){
redisTemplate.boundListOps("namelist2").leftPush("刘备");
redisTemplate.boundListOps("namelist2").leftPush("关羽");
redisTemplate.boundListOps("namelist2").leftPush("张飞");
}
/**
* 显示左压栈集合
*/
@Test
public void testGetValue2(){
List list = redisTemplate.boundListOps("namelist2").range(0, 10);
System.out.println(list);
}
//运行结果:
//[张飞, 关羽, 刘备]
//根据索引查询元素
/**
* 查询集合某个元素
*/
@Test
public void testSearchByIndex(){
String s = (String) redisTemplate.boundListOps("namelist1").index(1);
System.out.println(s);
}
//移除某个元素的值
/**
* 移除集合某个元素
*/
@Test
public void testRemoveByIndex(){
// 第一个参数 要删除元素的个数 (0全部删除) 第二个参数 元素值
redisTemplate.boundListOps("namelist1").remove(1, "关羽");
}
//Hash类型操作
//创建测试类TestHash
//存入值
@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 testGetKeys(){
Set s = redisTemplate.boundHashOps("namehash").keys();
System.out.println(s);
}
//运行结果:
//[a, b, c, d]
//提取所有的值
@Test
public void testGetValues(){
List values = redisTemplate.boundHashOps("namehash").values();
System.out.println(values);
}
//运行结果:
//[唐僧, 悟空, 八戒, 沙僧]
//根据KEY提取值
@Test
public void testGetValueByKey(){
Object object = redisTemplate.boundHashOps("namehash").get("b");
System.out.println(object);
}
//运行结果:
//悟空
//根据KEY移除值
@Test
public void testRemoveValueByKey(){
redisTemplate.boundHashOps("namehash").delete("c");
}
//运行后再次查看集合内容:
//[唐僧, 悟空, 沙僧]
5.项目中使用
5.2.2后端服务实现层
修改 youlexuan-content-service的ContentServiceImpl
|
5.3更新缓存
当广告数据发生变更时,需要将缓存数据清除,这样再次查询才能获取最新的数据
5.3.1新增广告后清除缓存
修改youlexuan-content-service工程ContentServiceImpl.java 的add方法
|
5.3.2修改广告后清除缓存
考虑到用户可能会修改广告的分类,这样需要把原分类的缓存和新分类的缓存都清除掉。
|
5.3.3删除广告后清除缓存
|
6.redis使用说明
1.Redis是一个缓存数据库,
2.redis的端口号是6379
连接远程redis服务器:
./redis-cli -h 192.168.75.13 -p 6379
在redis的命令行窗口:
shutdown:关闭服务器端
exit:退出客户端
3.redis的客户端:
命令行客户端:
图形化的客户端:
4.Redis中存储数据是通过key-value存储的,对于value的类型有以下几种:
字符串
哈希(hash)
字符串列表(list)
字符串集合(set)
有序字符串集合(sorted set)
5.字符串:
keys *:查询当前库中有哪些键
get key名称:获取该key对应的value值
set key名称 value值: 给指定的key设置value值
mset 键1 值1 键2 值2 键3 值3:同时为多个键设置值
mget 键1 键2 键3:同时获取多个键所对应的值
getset 键 值:先取值后设置值,相当于更新!
del 键的名称:删除指定的键值对
incr key1:给指定的key对应的value值加1
decr key1:给指定的key对应的value值减1
incrby key1 幅度:为指定key所对应的value值加指定幅度的数值
decrby key1 幅度:为指定key所对应的value值减去指定幅度的数值
APPEND key value:拼凑字符串,并返回字符串长度。
如果该key存在,则在原有的value后追加该值;如果该key不存在,则重新创建一个key/value
6.Hash类型:
hset key field1 value1: 为指定的key设置hash类型
hget key field1:获取指定键的哪个域所对应的值
hmset user username zhangsan age 12 gender true:为某个键同时设置多个filed-value值
hmget user username age:查看指定键多个field对应的值
HSETNX key field value:当字段不存在时赋值,类似HSET,区别在于如果字段存在,该命令不执行任何操作
hdel user gender:删除指定键的指定field
del user:删除指定键的所有filed
hincrby user age 3
HEXISTS key field : 判断指定的key中的filed是否存在,存在返回1,不存在返回0
hlen key: 获取key所包含的field的数量
hgetall key:获取该键所对应的hash数据【Map】
hkeys key:查看指定的key所对应的hash数据的field值
hvals key: 获取该键所对应的hash数据的value值
flushdb:清空当前库
flushall:清空所有库
select 0...15:切换库
7.List类型:有序,可重复
lpush key 1 2 3:从左边push到list中数据
rpush key value1 value2:从右边往list中添加数据
lpop key:从左边往外弹出数据
rpop key:从右边往外弹出数据
llen list1:指定的键list1对应的list有几个元素
lrange key 起始坐标 结束坐标: 查看指定key 从起始坐标到结束坐标的数据
8. set :无序,不可重复
sadd key value1 value2 value3:往指定的key中添加set数据
srem key value1:删除从指定key对应的set集合中的哪个数据
sismember key value1:查看指定key对应的set集合中是否存在value1值,如果存在,返回1,不存在返回0
smembers key:查看指定key中的所有set数据
sinter set1 set2:查看set1集合与set2集合中元素的交集
sdiff set1 set2:查看set1集合中有而set2集合中没有的元素
sunion set1 set2:查看set1集合与set2集合的并集
scard set1:查看该集合中有多少个元素
SRANDMEMBER key:随机返回set中的一个成员
type key:查看该key对应的value值是什么数据类型的
9.SortedSet:不可重复,根据score排序、有序的:
ZADD key score member [score member ...]
zrange math 0 -1:查看指定的sorttedset中的元素,根据每个元素的score从小到大的顺序排序
zrange math 起始下标 终止下标 withscores:查看指定的sorttedset中的元素,根据每个元素的score从小到大的顺序排序,并且带有score成绩
zrevrange math 起始下标 终止下标 withscores:查看指定的sorttedset中的元素,根据每个元素的score从大到小的顺序排序,并且带有score成绩
zscore key wangwu:查看指定key对应的sortedSet集合中的某个元素对应的score
ZREM key member [member ...]:从指定的key对应的sortedSet集合中删除某个元素
zincrby offcn:math 3 zhangsan: 修改指定key所对应的sortedset集合中某个元素的score值!
10.redis:
keys *:查看当前库中所有的键
type key: 查看键对应的值是什么数据类型:
expire key seconds:为指定的key设置过期时间
ttl key:查看当前key的剩余存活时间,返回值是正数:表示还有该正数的存活时间,-1表示该键是永久存活,-2表示已经过期【消失】
PERSIST key 清除生存时间
exists key:查看当前库中是否存在指定的key,返回值为1 表示存在,返回值为0表示不存在
del key:删除指定的key
rename age age_new:重命名key
11.
select 0 ...15: 切换当前库:
move key 库的索引:将指定的key移动到指定的库中
quit/exit:退出控制台
12.Redis 事务
在redis中,MULTI/EXEC/DISCARD这三个命令是我们实现事务的基石。
multi: begintransaction
exec: commit
discard: rollback
11.config get requirepass:获取当前密码
config set requirepass "123456" :设置当前密码为:123456
auth 密码
输入命令
config get dir:获取当前目录
12.命令介绍:
redis-benchmark:性能测试工具!
redis-check-aof:修复有问题的AOF文件!
redis-check-rdb:修复有问题的RDB文件!
redis-cli:Redis的客户端命令!(在客户端shell中执行config get requirepass 可以获取当前redis的密码,密码默认是空;config set requirepass "密码"就可以设置密码了,然后只要经过auth "密码"认证即可!)
redis-sentinel:Redis集群中会使用,哨兵模式!
redis-server:Redis服务器启动命令!
关闭redis服务器:在命令行中用shutdown命令
退出redis客户端:在命令行中用exit就好!
13.Redis 配置
Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf。
将daemonize no改为daemonize yes就可以将redis作为后台进程服务启动!
14.数据库的使用
Redis默认有16个数据库,
默认使用的是0号库,
使用select <DBID> 来切换库
使用DBSIZE来查看当前数据库的key的数量
Flushdb:来清空当前库
Flushall:清空所有的库
统一密码管理,16个库都是同样的密码,要么都能连接上,要么都连接不上
Redis库索引是从0开始的
补充:redis的默认端口号:6379
mongodb的默认端口号:27017
mysql的默认端口号:3306
ssh服务默认端口号:22
oracle默认端口号:1521
15.