0

Context

I'm making a game in ruby. It has a model named Character with attributes like energy and money. A Character can have a behavior, e.g. sleeping. Finally the Character has a tick method that calls its behavior method sleep!. Simplified it looks like this:

class Character
  attr_accessor :energy

  def initialize(behavior)
    @current_behavior = behavior
  end

  def tick
    self.send "#{@current_behavior}!"
  end

  private

    def sleep!
      self.energy -= 1 if energy > 0
    end

end

As in a lot of games the tick method of every Character needs to be invoked every n minutes. I want to use EventMachine for this. In a periodic timer it calls Character.tick_all that should invoke the tick method of every Character instance.

Question

What kind of persistence engine can I use for this? On server startup I want to load all Character instances in memory so they can be ticked. For now its ok if every instance gets persisted after its state changes because of a tick.

I've tried it with Rails and ActiveRecord. But it requires at least one write and read action for every tick which seems a bit of an overkill.

Edit I've looked into SuperModel. It seems to do exactly what I want, but it's last commit was about a year ago...

Flauwekeul
  • 869
  • 7
  • 24
  • hmm.. not sure if I understand the question, but eventually you could use Redis with a (sorted) set; every character has an ID, and you could store a key "123:energy", and zincrby value to achieve this. why is writing / reading from your data store a concern for you? – poseid May 07 '13 at 11:45
  • additionally you might want to look at the Moneta gem: https://github.com/minad/moneta – poseid May 07 '13 at 11:51
  • Redis is not a bad idea at all, but it offers way more functionality than I need. It's the same "problem" as with ActiveRecord: too much fuss for just some occasional persistence. – Flauwekeul May 07 '13 at 11:56
  • With ActiveRecord I need to save state constantly, with every tick, while that shouldn't be necessary. It should all be done in memory. The persistence is only needed as a backup. – Flauwekeul May 07 '13 at 12:01
  • the next simpler version to Redis would be Memcached. Just memory, have a key, and store some values under that key - once in a while store the keys to a file, but then you almost have a use case for Redis. – poseid May 07 '13 at 12:02
  • I think I go with redis on a Sinatra app then. Thanks! – Flauwekeul May 07 '13 at 13:47
  • Great to hear! Ok, If added an answer from the discussion above. If you like it, you can mark it as correct :-) Thanks! – poseid May 07 '13 at 19:47
  • I know it's been a while since this was asked, but I have a follow-on question about this - has anyone had good success with the 'persistence' gem, with the flat-file adaptor? http://rubydoc.info/gems/persistence/frames/file/README.md – pbr Aug 23 '13 at 19:30

1 Answers1

0

For simple lookups and storage of data in memory, there are 2 choices as far as I know:

  • Memcached: This is one of the most simple key-value stores around, and allows to easily SET and GET values that are associated with keys. However, as soon as you kill the process, all memories are flushed/destroyed, and it is a bit of a problem with storing data from users

  • Redis: Redis provides all the simple key-value lookup functionality of Memcached and much more. Apart from the fact, that is Redis is great of working with hashes, Redis provides set functionality (= not storing duplicates), and sorted set functionality (e.g. for scoring and ranking items). Also, Redis keeps track of data on the file-system and this is great when you need to restart a process once in a while.

poseid
  • 6,986
  • 10
  • 48
  • 78