lib/app/app/cache.rb in radiospiel-app-0.2.7 vs lib/app/app/cache.rb in radiospiel-app-0.2.9
- old
+ new
@@ -5,10 +5,43 @@
# stored away in a redis store.
#
# Entries are packed via Marshal.
module App
module Cache
+ class SqliteStore < MicroSql::KeyValueTable
+ def self.db_path
+ @db_path ||= begin
+ path = "#{Dir.home}/cache/#{File.basename(App.root)}/#{App.root.uid64}.sqlite3"
+ FileUtils.mkdir_p File.dirname(path)
+ path
+ end
+ end
+
+ def initialize
+ @db = MicroSql.create(SqliteStore.db_path)
+ super @db, "cache"
+ end
+
+ def get(key)
+ encoded = self[key]
+ Base64.decode64(encoded) if encoded
+ end
+
+ def set(key, value)
+ update key, Base64.encode64(value)
+ end
+
+ def expire(key, max_age)
+ ttl = max_age + Time.now.to_i if max_age
+ @db.ask("UPDATE cache SET ttl=? WHERE uid=?", ttl, key)
+ end
+
+ def flushdb
+ @db.exec "DELETE FROM cache"
+ end
+ end
+
DEFAULT_MAX_AGE = 4 * 3600 # 4 hours.
attr :store, true
extend self
@@ -29,21 +62,25 @@
}
end
end
def self.setup
- if !(cache_url = App.config[:cache])
- App.logger.warn "No :cache configuration"
- elsif !(self.store = connect_to_redis(cache_url))
- App.logger.warn "Using cache at #{cache_url}"
+ cache_url = App.config[:cache] || begin
+ App.logger.warn "No :cache configuration, fallback to #{SqliteStore.db_path}"
+ SqliteStore.db_path
end
- end
-
- def self.connect_to_redis(url)
- require "redis"
- Redis.connect(:url => url)
+
+ uri = URI.parse(cache_url)
+
+ self.store = case uri.scheme
+ when "redis"
+ require "redis"
+ Redis.connect(:url => cache_url)
+ when nil
+ SqliteStore.new
+ end
rescue LoadError
- App.logger.warn "Cannot load 'redis' gem (connecting to #{url})"
+ App.logger.warn "LoadError: #{$!}"
nil
end
end
def cached(key, max_age = Cache::DEFAULT_MAX_AGE, &block)