删除指定key
del key
获取key是否存在
exists key
获取key的类型
type key
为指定key设置有效期
expire key seconds
pexpire key milliseconds
expireat key timestamp
pexpireat key milliseconds-timestamp
获取key的有效时间
ttl key
pttl key
切换key从时效性转换为永久性
persist key
查询key
keys pattern
为key改名
rename key newkey
renamenx key newkey
对所有key排序
sort
其他key通用操作
help @generic
数据库通用操作
key 的重复问题
key是由程序员定义的。redis在使用过程中,伴随着操作数据量的增加,会出现大量的数据以及对应的key。数据不区分种类、类别混杂在一起,极易出现重复或冲突
redis为每个服务提供有16个数据库,编号从0到15,每个数据库之间的数据相互独立。
db 基本操作
切换数据库
select index
其他操作
quit
ping
echo message
数据移动
move key db
数据清除
dbsize
flushdb
flushall
Jedis
jar包导入
下载地址:https://mvnrepository.com/artifact/redis.clients/jedis
基于maven 引入依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
public class JedisTest {
@Test
public void testJedis(){
//1.连接redis
Jedis jedis = new Jedis("127.0.0.1", 6379);
//2.操作redis
jedis.set("name","xxp");
String name = jedis.get("name");
System.out.println(name);
//3.关闭redis
jedis.close();
}
@Test
public void testList(){
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.lpush("list2","a","b","c");
jedis.rpush("list2","d","e","f");
List<String> res = jedis.lrange("list2", 0, -1);
System.out.println("list2"+jedis.llen("list2"));
for (String tem:res){
System.out.println(tem);
}
jedis.close();
}
@Test
public void testHash(){
Jedis jedis = new Jedis("127.0.0.1", 6379);
jedis.hset("name","Sharpen","100");
jedis.hset("name","Giana","101");
Map<String, String> name = jedis.hgetAll("name");
for (Map.Entry<String,String> entry:name.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
System.out.println("lens:"+jedis.hlen("name"));
jedis.close();
}
}
public class Service {
String id;
int num;
public Service(String id,int num) {
this.id=id;
this.num=num;
}
//控制单元
public void service(){
Jedis jedis = new Jedis("127.0.0.1", 6379);
String value = jedis.get("compid"+id);
//判断是否为空
try{
if(value==null){
jedis.setex("compid"+id,5,Long.MAX_VALUE-num+"");
}else{
Long val = jedis.incr("compid"+id);
business(id,num-Long.MAX_VALUE+val);
}
}catch (JedisDataException e){
System.out.println("使用已经达到次数上限,请升级会员级别");
return;
}finally {
jedis.close();
}
}
//执行业务操作
public void business(String id,Long val){
System.out.println("用户"+id+"业务操作执行第"+val+"次");
}
}
class MyThread extends Thread{
Service service;
public MyThread(String id,int num) {
service=new Service(id,num);
}
@Override
public void run() {
while (true){
service.service();
// service.service();
try {
sleep(300L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Main{
public static void main(String[] args) {
MyThread thread1 = new MyThread("初级用户",10);
MyThread thread2 = new MyThread("高级用户",30);
thread1.start();
thread2.start();
}
}
Jedis简易工具类
基于连接池获取连接
JedisPool:Jedis提供的连接池技术
poolConfig:连接池配置对象
host:redis服务地址
port:redis服务端口号
public JedisPool(GenericObjectPoolConfig poolConfig, String host, int port) {
this(poolConfig, host, port, 2000, (String)null, 0, (String)null);
}
reids.properties
redis.host=127.0.0.1
redis.port=6379
redis.maxTotal=30
redis.maxIdle=10
public class JedisUtils {
private static JedisPool jedisPool = null;
static {
ResourceBundle rb = ResourceBundle.getBundle("redis");//加载redis.properties
String host = rb.getString("redis.host");
int port = Integer.parseInt(rb.getString("redis.port"));
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(Integer.parseInt(rb.getString("redis.maxTotal")));//设置一共连接数
poolConfig.setMaxIdle(Integer.parseInt(rb.getString("redis.maxIdle")));//活动连接数
jedisPool = new JedisPool(poolConfig, host, port);
}
public static Jedis getJedis() {
return jedisPool.getResource();
}
public static void main(String[] args) {
Jedis jedis = getJedis();
System.out.println(jedis);
}
}