前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Redis 多数据库2

Redis 多数据库2

作者头像
franket
发布2021-11-26 09:46:58
2380
发布2021-11-26 09:46:58
举报
文章被收录于专栏:技术杂记

名称空间

在不同库中设定的内容,并不会冲突

代码语言:javascript
复制
127.0.0.1:6379> select 10
OK
127.0.0.1:6379[10]> set a test
OK
127.0.0.1:6379[10]> select 11
OK
127.0.0.1:6379[11]> set a in11
OK
127.0.0.1:6379[11]> get a
"in11"
127.0.0.1:6379[11]> select 10
OK
127.0.0.1:6379[10]> get a
"test"
127.0.0.1:6379[10]> 

清空

FLUSHALL 是一个非常有破坏力的命令,因为它会清掉所有库中的数据

代码语言:javascript
复制
127.0.0.1:6379[10]> get a
"test"
127.0.0.1:6379[10]> select 11
OK
127.0.0.1:6379[11]> get a
"in11"
127.0.0.1:6379[11]> FLUSHALL
OK
127.0.0.1:6379[11]> get a
(nil)
127.0.0.1:6379[11]> select 10
OK
127.0.0.1:6379[10]> get a
(nil)
127.0.0.1:6379[10]>

使用CLI

代码语言:javascript
复制
[root@h102 redis-3.0.7]# ./src/redis-cli -p 6379 -n 13 set a in13 
OK
[root@h102 redis-3.0.7]# ./src/redis-cli -p 6379 -n 13 get a  
"in13"
[root@h102 redis-3.0.7]# ./src/redis-cli -p 6379 -n 11 get a  
(nil)
[root@h102 redis-3.0.7]#

使用API

使用API来操作,这里以ruby演示

先要安装一下gem

代码语言:javascript
复制
[root@h102 ruby]# ruby -v 
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
[root@h102 ruby]# gem install redis
Fetching: redis-3.2.2.gem (100%)
Successfully installed redis-3.2.2
Parsing documentation for redis-3.2.2
Installing ri documentation for redis-3.2.2
Done installing documentation for redis after 2 seconds
1 gem installed
[root@h102 ruby]#

在irb中进行操作

代码语言:javascript
复制
[root@h102 ruby]# irb 
2.3.0 :001 > require 'redis'
 => true 
2.3.0 :002 > redis = Redis.new(:host=>"localhost",:port=>6379,:db=>12)
 => #<Redis client v3.2.2 for redis://localhost:6379/12> 
2.3.0 :003 > redis.class
 => Redis 
2.3.0 :004 >
2.3.0 :005 > redis.methods
 => [:client, :with_reconnect, :without_reconnect, :connected?, :disconnect!, :auth, :ping, :bgrewriteaof, :bgsave, :get, :dbsize, :debug, :flushall, :flushdb, :quit, :info, :lastsave, :rename, :save, :shutdown, :slaveof, :slowlog, :persist, :expire, :expireat, :ttl, :pexpire, :pexpireat, :pttl, :migrate, :del, :exists, :move, :object, :randomkey, :renamenx, :decr, :decrby, :incr, :incrby, :incrbyfloat, :set, :setex, :psetex, :setnx, :mset, :mapped_mset, :msetnx, :mapped_msetnx, :mget, :mapped_mget, :setrange, :getrange, :setbit, :getbit, :append, :bitcount, :bitop, :bitpos, :getset, :strlen, :llen, :lpush, :lpushx, :rpush, :rpushx, :lpop, :rpop, :rpoplpush, :_bpop, :blpop, :brpop, :brpoplpush, :lindex, :linsert, :lrange, :lrem, :lset, :ltrim, :scard, :sadd, :srem, :[], :[]=, :monitor, :spop, :srandmember, :smove, :sismember, :smembers, :sdiff, :sdiffstore, :sinter, :sinterstore, :zcard, :sunionstore, :sunion, :zincrby, :zrem, :zadd, :zrange, :zscore, :zrevrange, :zrank, :zrevrank, :zremrangebyrank, :zrangebylex, :zrevrangebylex, :zrangebyscore, :zrevrangebyscore, :zremrangebyscore, :zcount, :zinterstore, :inspect, :method_missing, :zunionstore, :hlen, :hset, :hsetnx, :hmset, :mapped_hmset, :hget, :hmget, :mapped_hmget, :hdel, :hexists, :hincrby, :hincrbyfloat, :hkeys, :hvals, :hgetall, :publish, :subscribed?, :subscribe, :unsubscribe, :psubscribe, :punsubscribe, :pubsub, :watch, :unwatch, :pipelined, :multi, :discard, :script, :_eval, :evalsha, :_scan, :scan_each, :hscan, :hscan_each, :zscan, :zscan_each, :sscan, :sscan_each, :pfadd, :pfcount, :pfmerge, :dup, :sentinel, :keys, :type, :sort, :id, :select, :synchronize, :dump, :config, :restore, :scan, :exec, :sync, :echo, :time, :eval, :mon_try_enter, :try_mon_enter, :mon_enter, :mon_exit, :mon_synchronize, :new_cond, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :define_singleton_method, :is_a?, :singleton_method, :extend, :to_enum, :enum_for, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :display, :object_id, :send, :to_s, :method, :public_method, :nil?, :hash, :class, :singleton_class, :clone, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :==, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__] 
2.3.0 :006 > redis.methods.grep /set/
 => [:set, :setex, :psetex, :setnx, :mset, :mapped_mset, :msetnx, :mapped_msetnx, :setrange, :setbit, :getset, :lset, :hset, :hsetnx, :hmset, :mapped_hmset, :instance_variable_set] 
2.3.0 :007 > redis.set("testredis","rb")
 => "OK" 
2.3.0 :008 > redis.methods.grep /get/
 => [:get, :mget, :mapped_mget, :getrange, :getbit, :getset, :hget, :hmget, :mapped_hmget, :hgetall, :instance_variable_get] 
2.3.0 :009 > redis.get("testredis")
 => "rb" 
2.3.0 :010 > 

尝试在cli检索一下

代码语言:javascript
复制
[root@h102 redis-3.0.7]# ./src/redis-cli -p 6379 -n 12 get testredis
"rb"
[root@h102 redis-3.0.7]# ./src/redis-cli -p 6379 -n 13 get testredis
(nil)
[root@h102 redis-3.0.7]# 

注意

目前redis没法逆向关联数据,意思就是只能知道库里有哪些key,但没法知道一个key来自于哪个库,

这就要求应用自己去记录将数据放到了哪个库中,到时候依然得去那个库中取


命令汇总

  • ./src/redis-server --version
  • ./src/redis-cli -p 6379
  • cat redis.conf | grep -v "^#" | grep -v "^$"| grep databa
  • ./src/redis-cli -p 6379 -n 13 set a in13
  • ./src/redis-cli -p 6379 -n 13 get a
  • gem install redis
  • irb
  • ./src/redis-cli -p 6379 -n 12 get testredis

原文地址

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 名称空间
  • 清空
  • 使用CLI
  • 使用API
  • 注意
  • 命令汇总
相关产品与服务
云数据库 Redis®
腾讯云数据库 Redis®(TencentDB for Redis®)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档