class FileCache

Public Class Methods

new() click to toggle source
Calls superclass method Cache::new
# File lib/libcache/file_cache.rb, line 7
def initialize
  super
end

Public Instance Methods

create_store() click to toggle source

Initializes the cache store along with the keys store. Raises an exception if the store path is not supplied.

# 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) click to toggle source

Gets the object that corresponds with the key that is read from the filesystem @param [String] key The key value used to identify an object in the cache @return [Object] The object that corresponds with the key

# 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) click to toggle source

Deletes a key-value pair from the cache and store directory @param [String] key The key value used to identify an object in the cache

Calls superclass method Cache#invalidate
# 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() click to toggle source

Clears all items in the cache and the cached files in the store directory

Calls superclass method Cache#invalidate_all
# 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) click to toggle source

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. @param [String] key The key value used to identify an object in the cache @param [Object] value The object to be placed in the cache

# 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