简体   繁体   中英

How to change Rails redis cache format?

How do I change the method of storage on Rails when using the redis-rails gem. I set the cache store to use redis, and then used Rails.cache.fetch as documented. It works fully as expected, however when retrieving the key the output is not some easily editable format. Using redis-desktop-manager here is the output: http://imgur.com/cE1pAy1

(no sensitive info, randomly generated). Is there any way to have it stored as JSON? My end goal is to be able to connect to the redis server using Java and updating fields for specific users in the redis array.

Here is how I used the cache:

Rails.cache.fetch("some_key", expires_in: 1.day) do
 User.all.to_a.each do |user|
     user.name = "foobar"
 end

Later, from Java, I'd like to be able to connect to Redis and set some users name by ID or some other attribute. How can I either deserialize the Redis data OR store it in something like JSON to easily parse and change attributes?

EDIT Okay, I found a way to override the Marshalling code in redis-store (required by redis-rails) to use YAML, which I can deserialize. Now my next issue is this: Is it a bad use of redis to be storing a YAML file representing an array of users into a single key, and then to be updating the entire yaml file (for just one or two increment or decrement changes) often in Java?

The current YAML file is a list of users and their kills and deaths in a game written in Java. The Java game will need to often update a key in the YAML file, which means getting the entire YAML file, parsing it, changing a value, going back to a YAML format, and setting that as the key value. How inefficient is this exactly (should I be worrying) and how can I make the Rails redis prefer storing each user as a separate key if needed?

I just overrode the marshalling code. I reopened https://github.com/redis-store/redis-store/blob/master/lib/redis/store/marshalling.rb

And replaced the Marshall::dump with YAML::dump, along with the load

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM