How to store temporary data and share it with your background processor

In my current project, I had to store some temporary data for a user and let a few background processors have access to it. I wrote something small with a dependency on Redis which does the job.

It allows me to use current_user.tmp[:token_id] = "somedata here" and then access it in the background processor using user.tmp[:token_id] which I think is pretty neat.

Moreover, since my use case needed this for temporary storage, I set it to auto expire in 1 day. If yours is different you could change that piece of code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# /app/models/user_tmp.rb
class UserTmp
attr_reader :user
def initialize(user)
@user = user
end
EXPIRATION_SECONDS = 1.day
SERIALIZER = Marshal
def [](key)
serialized_val = Redis.current.get(namespaced_key(key))
SERIALIZER.load(serialized_val) if serialized_val
end
def []=(key, val)
serialized_val = SERIALIZER.dump(val)
Redis.current.setex(namespaced_key(key), EXPIRATION_SECONDS, serialized_val)
end
private
def namespaced_key(key)
"u:#{user.id}:#{key}"
end
end

And here is the user class

1
2
3
4
5
6
7
8
9
10
# /app/models/user.rb
class User < ActiveRecord::Base
#...
def tmp
@tmp ||= UserTmp.new(self)
end
#...
end

Hope you find it useful :)


I am currently working on Zammu which makes Automatic Deployment of static websites to Github Pages very easy. I would love to get your feedback on it, Use the invitation code KHAJA