cachet 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@ require 'rails'
3
3
  module Cachet
4
4
  mattr_accessor :logger
5
5
  mattr_accessor :storage
6
+ mattr_accessor :enabled
6
7
 
7
8
  class << self
8
9
  def setup
@@ -10,23 +11,28 @@ module Cachet
10
11
  end
11
12
 
12
13
  def cache (entity, key)
13
- cached_item = storage.read(entity, key)
14
- if not cached_item
15
- cached_item = yield
16
- storage.write(entity, key, cached_item)
17
- Cachet.logger.info "#{entity} with key:#{key} could not found in cache. Evaluated and written to cache"
14
+ if enabled
15
+ cached_item = storage.read(entity, key)
16
+ if not cached_item
17
+ cached_item = yield
18
+ storage.write(entity, key, cached_item)
19
+ Cachet.logger.info "#{entity} with key:#{key} could not found in cache. Evaluated and written to cache"
20
+ else
21
+ Cachet.logger.info "#{entity} with key:#{key} found in cache."
22
+ end
23
+ cached_item
18
24
  else
19
- Cachet.logger.info "#{entity} with key:#{key} found in cache."
25
+ yield
20
26
  end
21
- cached_item
22
27
  end
23
28
 
24
29
  def invalidate (entity, key)
25
- storage.purge(entity, key)
26
- Cachet.logger.info "#{entity} with key:#{key} invalidated."
30
+ if enabled
31
+ storage.purge(entity, key)
32
+ Cachet.logger.info "#{entity} with key:#{key} invalidated."
33
+ end
27
34
  end
28
35
  end
29
-
30
36
  end
31
37
 
32
38
  require 'cachet/file_store'
@@ -21,7 +21,9 @@ module Cachet
21
21
  define_method("#{cached_method}_with_cache_invalidation") { |*args|
22
22
  cache_key = block_given? ? yield(*args) : cached_method.to_s
23
23
  method("#{cached_method}_without_cache_invalidation".to_sym).call(*args)
24
- Cachet.invalidate(entity, cache_key)
24
+ (entity.kind_of?(Array) ? entity : [entity]).each do |ent|
25
+ Cachet.invalidate(ent, cache_key)
26
+ end
25
27
  }
26
28
  alias_method_chain "#{cached_method}".to_sym, :cache_invalidation
27
29
  Cachet.logger.debug "Aliasing method : #{cached_method} to invalidate the #{entity} entities"
@@ -9,7 +9,7 @@ module Cachet
9
9
 
10
10
  def initialize(root)
11
11
  @root = root
12
- FileUtils.mkdir_p root, :mode=>0755
12
+ FileUtils.mkdir_p root
13
13
  @optimize = TRUE
14
14
  @dir_level = 3
15
15
  @dir_count = 19
@@ -23,7 +23,7 @@ module Cachet
23
23
  def write(entity, key, data_to_be_stored)
24
24
  file_name = storage_path(entity, key)
25
25
  temp_file_name = file_name + '.' + Thread.current.object_id.to_s
26
- FileUtils.mkdir_p File.dirname(file_name), :mode => 755
26
+ FileUtils.mkdir_p File.dirname(file_name)
27
27
  File.open(temp_file_name, 'wb') { |f| f.write(Marshal.dump(data_to_be_stored)) }
28
28
  if File.exist?(file_name)
29
29
  File.unlink temp_file_name
@@ -43,6 +43,17 @@ module Cachet
43
43
  Cachet.logger.info "An element of #{entity} in the cache with key:#{key} to path #{file_name}entity has been removed from the cache."
44
44
  end
45
45
 
46
+ def entities
47
+ Dir.entries(root).reject { |entry| entry =='.' || entry == '..' }.inject({}) do |entities, entity|
48
+ entities[entity] = Dir["#{root}/#{entity}/**/*"].count { |file| File.file?(file) }
49
+ entities
50
+ end
51
+ end
52
+
53
+ def remove_entity(entity)
54
+ FileUtils.remove_dir File.join root, entity
55
+ end
56
+
46
57
  def storage_path(entity, key)
47
58
  file_name = Digest::MD5.hexdigest(key)
48
59
  if optimize
@@ -0,0 +1,55 @@
1
+ require_relative 'test_helper'
2
+
3
+ module Cachet
4
+ mattr_accessor :cache_calls
5
+ mattr_accessor :invalidate_calls
6
+
7
+ def self.cache (entity, key)
8
+ @@cache_calls = ((@@cache_calls or []) << [entity, key])
9
+ end
10
+
11
+ def self.invalidate (entity, key)
12
+ @@invalidate_calls = ((@@invalidate_calls or []) << [entity, key])
13
+ end
14
+ end
15
+
16
+ class CacheableTest < Test::Unit::TestCase
17
+ include Cachet::Cacheable
18
+
19
+ def setup
20
+ end
21
+
22
+ def method_cached(param1, param2)
23
+ return [param1, param2].join('-')
24
+ end
25
+
26
+ def method_invalidator(param1, param2)
27
+
28
+ end
29
+
30
+ def test_cacheable
31
+ self.method_cached 'umut', 'utkan'
32
+ assert_equal Cachet.cache_calls.length, 1
33
+ cache_call = Cachet.cache_calls[0]
34
+ assert_equal :foo, cache_call[0]
35
+ assert_equal 'umut-utkan', cache_call[1]
36
+
37
+ self.method_invalidator 'umut', 'utkan'
38
+ assert_equal Cachet.invalidate_calls.length, 2
39
+ invalidate_call = Cachet.invalidate_calls[0]
40
+ assert_equal :foo, invalidate_call[0]
41
+ assert_equal 'umut/utkan', invalidate_call[1]
42
+ invalidate_call = Cachet.invalidate_calls[1]
43
+ assert_equal :bar, invalidate_call[0]
44
+ assert_equal 'umut/utkan', invalidate_call[1]
45
+ end
46
+
47
+ cacheable :method_cached, :foo do |param1, param2|
48
+ [param1, param2].join '-'
49
+ end
50
+
51
+ cache_invalidator :method_invalidator, [:foo, :bar] do |param1, param2|
52
+ [param1, param2].join '/'
53
+ end
54
+
55
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: cachet
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.0
5
+ version: 0.0.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Meltem Atesalp
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2012-02-07 00:00:00 +02:00
14
+ date: 2012-02-27 00:00:00 +02:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -51,6 +51,7 @@ files:
51
51
  - README
52
52
  - test/test_file_store.rb
53
53
  - test/test_helper.rb
54
+ - test/test_cacheable.rb
54
55
  has_rdoc: true
55
56
  homepage: https://github.com/meltem/cachet
56
57
  licenses: []
@@ -82,3 +83,4 @@ summary: Caches method responses to have a better performance!
82
83
  test_files:
83
84
  - test/test_file_store.rb
84
85
  - test/test_helper.rb
86
+ - test/test_cacheable.rb