简介
redis是一种高级的key:value存储系统,其中value支持五种数据类型:
1.字符串(strings)
2.字符串列表(lists)
3.字符串集合(sets)
4.有序字符串集合(sorted sets)
5.哈希(hashes)
而关于key,有几个点要提醒大家:
1.key不要太长,尽量不要超过1024字节,这不仅消耗内存,而且会降低查找的效率;
2.key也不要太短,太短的话,key的可读性会降低;
3.在一个项目中,key最好使用统一的命名模式,例如user:10000:passwd。
使用
1,登录redis
redis-cli -h ip地址;
2,选择数据库(共有16个,从0开始)
select 数据库编号
3,存取删值(这里name为key,hello为value)
#存
set name hello
#取
get name
#删
del name
4,hash
#存(cn是一个大key,no1 no2 no3 为小key)
hmset cn no1 zs no2 ls no3 ww
#取no1
HGET cn no1
#删整个
del cn
#删某个
hdel cn no1
#查所有
hgetall cn
5,列表(list)
#存
lpush x 1 2 3 4
#取(0是起始,2是停止位置,这里存取类似于压栈,先进后出,所以拿出来的值是反过来的)
LRANGE x 0 2
6,set集合
#存(会自动去重)
sadd y 1 1 2 2 3
#取
SMEMBERS y
7,有序集合(sorted set)
zadd z 0 zs
zadd z 1 ls
zadd z 2 ww
#取(0-100范围)
ZRANGEBYSCORE z 0 1000
8,查看key是否存在
exists key值
9,给key设置过期时间(3秒后过期,就自动消失了)
10,查询所有大key
keys *
11,将当前数据库的key移到另一个库
move key值 另一个库值
12,修改key
rename 当前key 要改成的名字
13, 将key中存取的数字值增1(incr),减1(decr)
14,如果key已经存在并且是一个字符串,append命令将指定的value追加到key原来值的(value)的末尾
15,hash命令(查看key是否存在)
16,list命令(blpop:弹出并移出,)
通过索引取值
在列表的元素前或后插入值
获取列表长度
lrem移出列表元素,lset通过索引设定值
ltrim 后面负数-2 倒数着删
17,set命令
返回两集合的交集
返回两集合的并集(自动去重)
java操作redis
maven项目
1,添加依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
2,连接并查询
public class RedisApplication {
public static void main(String[] args) {
Jedis jedis = new Jedis("192.168.88.120", 6379);
// jedis.select(15);
System.out.println(jedis.hgetAll("names"));
}
}
springboot项目
1,添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2,yml配置
spring:
redis:
host: 192.168.88.120
port: 6379
database: 0
password:
3,service层
package com.example.myred02.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class DataService {
@Autowired
private StringRedisTemplate template;
public String xx(){
// 动态选择数据库
LettuceConnectionFactory lcf = (LettuceConnectionFactory) template.getConnectionFactory();
lcf.setDatabase(15);
template.setConnectionFactory(lcf);
//连接重启数据库
lcf.afterPropertiesSet();
String aaa = template.opsForValue().get("aaa");
return aaa;
}
}
4,controller层
package com.example.myred02.controller;
import com.example.myred02.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class InitCtrl {
@Autowired
private DataService ds;
@RequestMapping("/test")
public String tests(){
return ds.xx();
}
}