开发者

How to set and get an object stored in Redis?

开发者 https://www.devze.com 2023-03-28 03:20 出处:网络
I\'m trying to store an object in redis, and when I get the object it doesn\'t seem to work. I tried: u = User.new

I'm trying to store an object in redis, and when I get the object it doesn't seem to work.

I tried:

u = User.new
u.name = 'blankman'

$redis.set('test', u)

x = $redis.get('test')

x.name开发者_JAVA技巧  // error

I guess it is a serialization issue, do I have to do something special to my model for it to serialize?


I would suggest using the Marshal.dump and Marshal.load to serialize and deserialize objects respectively.

$redis.set 'key', (Marshal.dump object) #serialize
object = Marshal.load($redis.get 'key') #deserialize


As far as I know, redis will save a String representation of your object. So...

x.class # String

You have to use something to serialize/deserialize your objects when using Redis. Take a look at https://github.com/nateware/redis-objects

Kind Regards

0

精彩评论

暂无评论...
验证码 换一张
取 消