Reinier Reinier - 3 months ago 18
Ruby Question

Alternative for storing information between sessions similar to redis

For my project I use an external API. On every call the API provides a token so that the next time you request data they don't have to dump all data but just the updates. I would like this data to persist between different server sessions in development. On my mac OS I use redis for such things and store the tokens under keys, I am currently developing on windows and can not install any type of redis alternative, what would be best practice for these scenarios? Should I store this information in the DB or are there more efficient and cleaner ways?

Answer

Based on mwp's answer I used the following which works for me:

require 'pstore' on top of my module, I wrote two methods, one to save a token and one to retrieve a token:

def get_event_token
    tokens = PStore.new("tokens.pstore")
    event_token = ""
    tokens.transaction(true) do 
        event_token = tokens.fetch(:event_token).to_s
    end
    event_token
end

def store_event_token(event_token)
    tokens = PStore.new("tokens.pstore")
    tokens.transaction do 
        tokens[:event_token] = event_token
    end
end

For retrieving make sure that you define the event_token outside of the transaction, because for some reason the scope of the transaction seems limited to the transaction

Comments