Permalink
Browse files

logging

  • Loading branch information...
phoet committed Oct 27, 2009
1 parent 079c398 commit f80e1a02d7cd005f059c3217b7bbf387f8c8fdca
Showing with 44 additions and 7 deletions.
  1. +33 −7 lib/rails_file_cache.rb
  2. +11 −0 test/test_rails_file_cache.rb
View
@@ -1,45 +1,64 @@
require 'activesupport'
require 'file_helper'
+# = RailsFileCache
+#
+# Simple Caching implementation that persists the content into a temporary file.
+#
+# The only cool stuff is that you can optinally define the time to
+# live via the option +:expires_in+ which defaults to 30.seconds if not set.
+#
+# == Example
+#
+# cache.fetch(@key, :expires_in=>30.seconds) do
+# some_action_that_should_be_cached_for_configured_time
+# end
+#
module ActiveSupport
module Cache
class RailsFileCache < Store
+ # reads cached value for key
+ # default-caching for 30.seconds can be configured through the +:expires_in+ option
def read(key, options = {})
log("read", key, options)
name = file_name key
expires_in = (options[:expires_in] || 30.seconds)
- puts "reading from file=#{name} with expires=#{expires_in}"
+ log("reading from file=#{name} with expires=#{expires_in}", key, options)
File.read(name) if FileHelper.not_cached_or_to_old?(name, expires_in)
end
+ # writes value to tmp-file and returns the value again
def write(key, value, options = nil)
log("write", key, options)
name = file_name key
- puts "writing value=#{value} to file=#{name}"
+ log("writing value=#{value} to file=#{name}", key, options)
File.open(name, "w") { |file| file << value }
value
end
+ # deletes the cached value for given +key+
def delete(key, options = nil)
log("delete", key, options)
name = file_name key
- puts "deleting file=#{name}"
+ log("deleting file=#{name}", key, options)
File.delete name if File.exists? name
end
+ # deletes the cached value for all keys matching +matcher+
def delete_matched(matcher, options = nil)
log("delete matched", matcher.inspect, options)
Dir[FileHelper.tmpdir + "/*.#{FileHelper.extension}"].grep(matcher) do |match|
- puts "deleting file=#{match}"
+ log("deleting file=#{match}", matcher.inspect, options)
File.delete match
end.size
end
+ # checks if a cached value exists for given +key+
def exist?(key, options = nil)
log("exist?", key, options)
name = file_name key
- puts "checking file=#{name} exists"
+ log("checking file=#{name} exists", key, options)
File.exists? name
end
@@ -50,9 +69,16 @@ def file_name(key)
end
def log(operation, key, options)
- puts("Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}")
+ if !@silence && !@logger_off
+ message = "Cache #{operation}: #{key}#{options ? " (#{options.inspect})" : ""}"
+ if logger
+ logger.debug message
+ else
+ puts message
+ end
+ end
end
-
+
end
end
end
@@ -17,6 +17,17 @@ def test_read_write
assert_equal(@value, @cache.read(@key, @options))
assert_equal(nil, @cache.read(@key, :expires_in=>0.seconds))
end
+
+
+ def test_fetch
+ count = 1
+ @cache.fetch(@key, :expires_in=>30.seconds) {count += 1}
+ @cache.fetch(@key, :expires_in=>30.seconds) {count += 1}
+ @cache.fetch(@key, :expires_in=>30.seconds) {count += 1}
+ assert_equal(count, 2)
+ @cache.fetch(@key, :expires_in=>0.seconds) {count += 1}
+ assert_equal(count, 3)
+ end
def test_write
assert_equal(@value, @cache.write(@key, @value, @options))

0 comments on commit f80e1a0

Please sign in to comment.