Redis 多数据库2

2021-11-26 09:46:58 浏览数 (1)

名称空间

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

代码语言: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

原文地址

0 人点赞