lib/libcache/file_cache.rb in libcache-0.3 vs lib/libcache/file_cache.rb in libcache-0.4

- old
+ new

@@ -21,45 +21,43 @@ # @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 def put(key, value) raise InvalidKey unless key.is_a? String raise InvalidKey unless key =~ /\A[a-zA-Z0-9_-]+\z/ - if max_size != nil - if @cache.size >= max_size - 1 - key, value = @time_tracker.values.sort {|v| Time.now - v }.reverse.first - invalidate(key) - @time_tracker.delete(key) - end - @time_tracker[key] = Time.now + @keys[key] = @keys.size.to_s + File.open(File.join(store, @keys[key]), 'w') do |f| + f.write(Marshal.dump(value)) end - @keys[key] = Digest::MD5.hexdigest(key) + Time.now.to_i.to_s @cache[key] = value - File.open(File.join(store, @keys[key]), 'w') do |f| - f.write(value) + if expiry_time != nil + @scheduler.in expiry_time, :blocking => true do + invalidate key + end end - @scheduler.in expiry_time, :blocking => true do - invalidate key - end + check_expiration(key) end # 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 def get(key) - refresh - return File.read(File.join(store, @keys[key])) + check_refresh(key) + if(@keys[key]) == nil + return nil + end + return Marshal.load(File.read(File.join(store, @keys[key]))) end # 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 def invalidate(key) super - @keys.delete key File.delete(File.join(store, @keys[key])) + @keys.delete key end # Clears all items in the cache and the cached files in the store directory - def invalidateAll + def invalidate_all super Dir.foreach(store) do |f| File.delete(File.join(store, f)) if f != '.' && f != '..' end end \ No newline at end of file