Class: FileCache
Instance Attribute Summary
Attributes inherited from Cache
#expiry_time, #max_size, #post_get, #refresh, #store
Instance Method Summary collapse
-
#create_store ⇒ Object
Initializes the cache store along with the keys store.
-
#get(key) ⇒ Object
Gets the object that corresponds with the key that is read from the filesystem.
-
#initialize ⇒ FileCache
constructor
A new instance of FileCache.
-
#invalidate(key) ⇒ Object
Deletes a key-value pair from the cache and store directory.
-
#invalidate_all ⇒ Object
Clears all items in the cache and the cached files in the store directory.
-
#put(key, value) ⇒ Object
Places an object inside the cache, and by extension, into the filesystem.
Methods inherited from Cache
#check_refresh, #exists?, #get_size, #has_refresh?, #perform_post_get
Constructor Details
#initialize ⇒ FileCache
Returns a new instance of FileCache.
7 8 9 |
# File 'lib/libcache/file_cache.rb', line 7 def initialize super end |
Instance Method Details
#create_store ⇒ Object
Initializes the cache store along with the keys store. Raises an exception if the store path is not supplied.
12 13 14 15 16 17 18 |
# File 'lib/libcache/file_cache.rb', line 12 def create_store @cache = Hash.new @keys = Hash.new if store == nil raise 'Store path is missing!' end end |
#get(key) ⇒ Object
Gets the object that corresponds with the key that is read from the filesystem
42 43 44 45 46 47 48 49 |
# File 'lib/libcache/file_cache.rb', line 42 def get(key) check_refresh(key) if(@keys[key]) == nil return nil end perform_post_get(key) return Marshal.load(File.read(File.join(store, @keys[key]))) end |
#invalidate(key) ⇒ Object
Deletes a key-value pair from the cache and store directory
53 54 55 56 57 |
# File 'lib/libcache/file_cache.rb', line 53 def invalidate(key) super File.delete(File.join(store, @keys[key])) @keys.delete key end |
#invalidate_all ⇒ Object
Clears all items in the cache and the cached files in the store directory
60 61 62 63 64 65 |
# File 'lib/libcache/file_cache.rb', line 60 def invalidate_all super Dir.foreach(store) do |f| File.delete(File.join(store, f)) if f != '.' && f != '..' end end |
#put(key, value) ⇒ Object
Places an object inside the cache, and by extension, into the filesystem. Handles max size eviction. Raises an InvalidKey exception if the key is not formatted properly.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/libcache/file_cache.rb', line 23 def put(key, value) raise ArgumentError unless key.is_a? String raise ArgumentError unless key =~ /\A[a-zA-Z0-9_-]+\z/ @keys[key] = @keys.size.to_s File.open(File.join(store, @keys[key]), 'w') do |f| f.write(Marshal.dump(value)) end @cache[key] = value if expiry_time != nil @scheduler.in expiry_time, :blocking => true do invalidate key end end check_expiration(key) end |