Last active
July 13, 2018 15:38
-
-
Save pachacamac/54efea9abcde6d34fa00 to your computer and use it in GitHub Desktop.
a super simple pstore based key value store for minimal db-needs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pstore' | |
class Store | |
def initialize(file, thread_safe = true) | |
@store = PStore.new(file, thread_safe) | |
end | |
def crud(key=nil, val=nil) | |
val ? @store.transaction{ @store[key] = val } | |
: @store.transaction(false){ key ? @store[key] : Hash[@store.roots.map{|k| [k, @store[k]]}] } | |
end | |
end | |
mydb = Store.new('db.pstore') | |
# save something to the store | |
mydb.crud(:foo, 'bar') | |
# read something from the store | |
mydb.crud(:foo) # 'bar' | |
mydb.crud(:xxx) # nil | |
# get the whole store as a Hash | |
mydb.store # {:foo => 'bar'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment