1 Redis入门
Redis优点:性能很好、支持的数据很完善、使用简单
Redis应用:
1.缓存:使用非常频繁的数据,可以放在内存上
2.排行榜:访问非常频繁的帖子,放在内存上
3.计数器:每访问一下帖子,浏览量+1,这个动作很频繁,用Redis实现
4.社交网络:点赞、点踩、关注,动作很频繁,这种数据存在数据库里也不好存,以键值对的方式存在内存里
5.消息队列:可以满足要求,但不是专门的
Redis主要学数据类型,会存数据类型就ok了,用各种命令
下载win版:https://github.com/microsoftarchive/redis
管理员cmd
redis-cli
select 1#内置16个库
flushdb#不想要数据了,刷新库,把内容刷没
#Redis里两个相连的单词用:隔开
set test:count 1#存String类型数据
get test:count
incr test:count#+1
decr test:count#-1
hset test:user id 1#存Hash类型数据
hget test:user id
#list数据:横向容器,左进右出,有序
lpush test:ids 101 102 103
llen test:ids
lindex test:ids 0
lrange test:ids 0 2
rpop test:ids
#集合:无序,无重复数据
sadd test:teachers aaa bbb ccc ddd eee
scard test:teachers#统计
spop test:teachers#随机弹出一个数据
smembers test:teachers
#有序集合:给每个存的值附加一个分数,用分数排序
zadd test:students 10 aaa 20 bbb 30 ccc
zscore test:students ccc#查分数
zrank test:students ccc
zrange test:students 0 2
keys *
keys test*
type test:user
exists test:user
del test:user
expire test:students 10#有效期10秒
2 Spring整合Redis
#RedisProperties
spring.redis.database=11
spring.redis.host=localhost
spring.redis.port=6379
@Configuration
public class RedisConfig {
@Bean//定义第三方Bean,Bean名字为redisTemplate
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
//注入连接工厂才能链接数据库
RedisTemplate<String,Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);//可以访问数据库了
//设置key的序列化方式
template.setKeySerializer(RedisSerializer.string());
//设置value的序列化方式
template.setValueSerializer(RedisSerializer.json());
//设置hash的key的序列化方式
template.setHashKeySerializer(RedisSerializer.string());
//设置hash的value的序列化方式
template.setHashValueSerializer(RedisSerializer.json());
template.afterPropertiesSet();//设置完之后,触发生效
return template;
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class RedisTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testStrings(){
String redisKey = "test:count";
redisTemplate.opsForValue().set(redisKey, 1);
System.out.println(redisTemplate.opsForValue().get(redisKey));
System.out.println(redisTemplate.opsForValue().increment(redisKey));
System.out.println(redisTemplate.opsForValue().decrement(redisKey));
}
@Test
public void testHash(){
String redisKey = "test:user";
redisTemplate.opsForHash().put(redisKey, "id", 1);
redisTemplate.opsForHash().put(redisKey,"username","zhangsan");
System.out.println(redisTemplate.opsForHash().get(redisKey, "id"));
System.out.println(redisTemplate.opsForHash().get(redisKey, "username"));
}
@Test
public void testLists(){
String redisKey = "test:ids";
redisTemplate.opsForList().leftPush(redisKey, 101);
redisTemplate.opsForList().leftPush(redisKey, 102);
redisTemplate.opsForList().leftPush(redisKey, 103);
System.out.println(redisTemplate.opsForList().size(redisKey));
System.out.println(redisTemplate.opsForList().index(redisKey, 0));
System.out.println(redisTemplate.opsForList().range(redisKey, 0, 2));
System.out.println(redisTemplate.opsForList().leftPop(redisKey));
System.out.println(redisTemplate.opsForList(