创建Redis对象并在调用方法时执行命令时出错

时间:2014-03-24 08:11:12

标签: redis redis-cache

在我包含rubygems和redis之后,这是我的rails控制台的输出。

2.0.0-p353 :024 > r = Redis.new
=> #<Redis client v3.0.7 for redis://127.0.0.1:6379/0> 
2.0.0-p353 :025 > r.set('foo','bar')
=> "OK" 
2.0.0-p353 :026 > r.get('foo')
=> "bar"
2.0.0-p353 :033 > r.lpush('foo','bar')
Redis::CommandError: ERR Operation against a key holding the wrong kind of value
from /home/poorva/.rvm/gems/ruby-2.0.0-p353/gems/redis-3.0.7/lib/redis/client.rb:97:in `call'
from /home/poorva/.rvm/gems/ruby-2.0.0-p353/gems/redis-3.0.7/lib/redis.rb:949:in `block in lpush'
from /home/poorva/.rvm/gems/ruby-2.0.0-p353/gems/redis-3.0.7/lib/redis.rb:37:in `block in synchronize'
from /home/poorva/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
from /home/poorva/.rvm/gems/ruby-2.0.0-p353/gems/redis-3.0.7/lib/redis.rb:37:in `synchronize'
from /home/poorva/.rvm/gems/ruby-2.0.0-p353/gems/redis-3.0.7/lib/redis.rb:948:in `lpush'
from (irb):33
from /home/poorva/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /home/poorva/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /home/poorva/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.0.0-p353 :034 > r.lpush(foo,'bar')
NameError: undefined local variable or method `foo' for main:Object
from (irb):34
from /home/poorva/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /home/poorva/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /home/poorva/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'

1 个答案:

答案 0 :(得分:-1)

更新:第一个参数必须是lpush命令的第一个参数,也就是说列表的键。第二,要推的价值。


初始消息(在修改问题之前)

邮件是明确的ArgumentError: wrong number of arguments (0 for 2)。只需向LPUSH提供Redis documentation中描述的预期参数:键和要推送的值。

lpush('mylist', 'bar')