Redis新手学习教程
【摘要】
在bash中和Java中使用Redis
命令行中使用方法模拟报文发送用Telnet进行模拟客户端发送
批量键值对
命令行中使用方法
初次进入
mxd@mxd-TM1613:~$ redis-cli //进入
127.0.0.1:6379> AUTH 110 //输入密码
OK
123
模拟报文发送
用Telnet进行模拟客户端发...
命令行中使用方法
- 初次进入
mxd@mxd-TM1613:~$ redis-cli //进入
127.0.0.1:6379> AUTH 110 //输入密码
OK
- 1
- 2
- 3
模拟报文发送
用Telnet进行模拟客户端发送
*表示开始,3是参数数量
。。。。。。。
Redis 默认有16个数据库,即16个database
Select 【数字】 转到第几个数据库
Dbsize 查看当前数据库有多少个key
Keys * 当前库的key查询出来
Flushdb 清除当前库
Flushall 清除所有库
Auth 【密码】 认证密码
Exists【key的名字】 判断某个key是否存在
Move key 【db序号】 将当前库的key剪切到指定db序号
Expire key【时间秒】 为给定的key设置过期时间,过期后会移除系统
Ttl key 查看还有多少秒过期,-1表示永远不过期,-2表示已过期
Type key 查看你的key是什么类型
Del key 删除key
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
批量键值对
mget key1 key2 // 返回一个列表
mset key1 m key2 x key2 d //分别给三个key赋值m x d
expire key1 5 //5s后key1 过期
setex key1 5 m //设定key的时候就把过期时间加上
SETNX key1 m //如果key1不存在就执行set创建
set age 30
incr age //age自增1
incrby age 5 //age自增5
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 查看进程,杀死进程
ps -ef | grep redis 可以查看正在运行的redis 进程
kill -9 XXX 也可以杀死进程
- 1
- 2
- 3
#lettuce使用redis
package redisTset;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.sync.RedisCommands;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import static java.lang.System.in;
public class LettuceClient { public static RedisURI redisUri = null; public static RedisClient client = null; public static StatefulRedisConnection<String, String> connect = null; public static void main(String[] args) throws Exception { LettuceClient lettuceClient = new LettuceClient(); //进行string类型的crud lettuceClient.StringTest(); //进行list类型的crud lettuceClient.ListTest(); // 进行list类型的crud lettuceClient.hashTest(); // 进行list类型的crud lettuceClient.setTest(); //进行list类型的crud lettuceClient.zsetTest(); /* //异步方式 RedisAsyncCommands<String, String> redisAsync = connect.async(); RedisFuture<String> redisFuture = redisAsync.get("age"); try { String a = redisFuture.get(); System.out.println("异步" + a); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } connect.close(); client.shutdown();*/ } void StringTest() { open(); //同步方式进行String类型增删改查 RedisCommands<String, String> commands = connect.sync(); Scanner scanner = new Scanner(in); System.out.println("进行insert操作,请输入key:"); String key = scanner.next(); System.out.println("进行insert操作,请输入value:"); String next1 = scanner.next(); if ("OK" == commands.set(key, next1)) { System.out.println(" insert success "); } else System.out.println(" insert fail "); System.out.println("进行delete操作,请输入key:"); key = scanner.next(); if (commands.del(key) == 1) { System.out.println("success delete"); } System.out.println("进行update操作,请输入key:"); key = scanner.next(); if (commands.get(key) != null) { System.out.println("进行update操作,请输入value:"); if ("OK" == commands.set(key, scanner.next())) { System.out.println("update success"); } } else { System.out.println("要update的key不存在"); } //查询全部key System.out.println("查询全部key:"); List<String> keys = commands.keys("*"); for (String s : keys) { System.out.print(s + " "); } close(); } void ListTest(){ open(); //同步方式进行String类型增删改查 RedisCommands<String,String> commands=connect.sync(); String key="books"; String[] books={"java","py","goLang"}; String[] books2={"java2","py2","goLang2"}; commands.rpush(key,books); //出队一个 System.out.println("出队一个"+ commands.lpop(key)); //出栈一个 System.out.println("出队一个"+ commands.rpop(key)); commands.rpush(key,books); //遍历 commands.lindex(key ,-1); //update commands.lset(key,0,"JAVA"); close();
}
void hashTest(){ open(); //同步方式进行String类型增删改查 RedisCommands<String,String> commands=connect.sync(); String key="hello"; //insert commands.hset(key,"java","hello,java"); commands.hset(key,"py","hello ,py"); commands.hset(key,"goLong","hello,hoLong"); //delete commands.hdel(key,"java"); //update commands.hset(key,"java","HELLO,JAVA"); //query Map<String, String> hgetall = commands.hgetall(key); for (Map.Entry<String, String> stringStringEntry : hgetall.entrySet()) { System.out.println(stringStringEntry); }
close(); } void setTest(){ open(); //同步方式进行String类型增删改查 RedisCommands<String,String> commands=connect.sync(); String key="bookss"; //insert commands.sadd(key,"py"); commands.sadd(key,"java","go"); //delete commands.spop(key); //update就是先删除在增加 //query Set<String> smembers = commands.smembers(key); for (String smember : smembers) { System.out.println(smember); } close(); } void zsetTest(){ open(); //同步方式进行String类型增删改查 RedisCommands<String,String> commands=connect.sync(); String key="booksss"; //insert commands.zadd(key,9.0 ,"hi,java"); commands.zadd(key,5.0 ,"hi,py"); commands.zadd(key,12.0 ,"hi,go"); //delete commands.zrem(key,"hi,py"); //query,zrange是正序,zrevrange是逆序 List<String> zrange = commands.zrange(key, 0, -1); System.out.println(zrange); close(); } public static void open() { //设置URL,以及密码,和哪个库 redisUri = RedisURI.Builder.redis("localhost") .withPassword("110").withDatabase(0) .build(); // //建立连接 client = RedisClient.create(redisUri); connect = client.connect(); } public static void close() { connect.close(); client.shutdown(); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
文章来源: blog.csdn.net,作者:East Horse,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_44613138/article/details/105127698
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)