lib/plezi/common/cache.rb in plezi-0.10.8 vs lib/plezi/common/cache.rb in plezi-0.10.9

- old
+ new

@@ -1,25 +1,30 @@ module Plezi # File and Object Caching for Plezi + # + # Be aware that the cache is local, per process. It's possible to limit file caching by limiting the supported_types in Plezi::Cache::CACHABLE. + # + # Template rendering engines are always cached. module Cache # contains the cached data, in the format: CACHE_STORE["filename"] = CacheObject CACHE_STORE = {} CACHE_LOCK = Mutex.new - CACHABLE = %w{cache object slim haml css map js html scss sass coffee txt xml json yaml rb} + CACHABLE = (%w{cache object slim haml css map js html scss sass coffee txt xml json yaml rb}).to_set @cache_to_disk = true # this class holds cached objects (data and modification times) class CacheObject # Cached attributes attr_accessor :data, :mtime # initialize a Cached object def initialize d = nil, t = Time.now - @data, @mtime = d, t + @data = t + @mtime = d end end # load the file from the cache (if exists) or the file system (if it doesn't) def load_file filename @@ -32,19 +37,19 @@ end # force a file onto the cache (only if it is cachable - otherwise will load the file but will not cache it). def reload_file filename if CACHABLE.include? filename.match(/\.([^\.]+)$/)[1] - return cache_data filename, IO.read(filename), File.mtime(filename) + return cache_data filename, IO.binread(filename), File.mtime(filename) else - return IO.read(filename) + return IO.binread(filename) end end # places data into the cache, and attempts to save the data to a file name. def save_file filename, data, save_to_disk = false cache_data filename, data if CACHABLE.include? filename.match(/\.([^\.]+)$/)[1] begin - IO.write filename, data if save_to_disk + IO.binwrite filename, data if save_to_disk rescue Exception => e Plezi.warn("File couldn't be written (#{filename}) - file system error?") end data end