Class: Gemirro::Cache
- Inherits:
-
Object
- Object
- Gemirro::Cache
- Defined in:
- lib/gemirro/cache.rb
Overview
The Cache class contains all method to store marshal informations into files.
Instance Attribute Summary collapse
- #root_path ⇒ String readonly
Instance Method Summary collapse
-
#cache(key) ⇒ Mixed
Cache data.
-
#create_root_path ⇒ Object
Create root path.
-
#flush ⇒ Object
Flush cache directory.
-
#flush_key(key) ⇒ Object
Flush key.
-
#initialize(path) ⇒ Cache
constructor
Initialize cache root path.
Constructor Details
#initialize(path) ⇒ Cache
Initialize cache root path
17 18 19 20 |
# File 'lib/gemirro/cache.rb', line 17 def initialize(path) @root_path = path create_root_path end |
Instance Attribute Details
#root_path ⇒ String (readonly)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gemirro/cache.rb', line 9 class Cache attr_reader :root_path ## # Initialize cache root path # # @param [String] path # def initialize(path) @root_path = path create_root_path end ## # Create root path # def create_root_path FileUtils.mkdir_p(@root_path) end ## # Flush cache directory # def flush FileUtils.rm_rf(@root_path) create_root_path end ## # Flush key # # @param [String] key # def flush_key(key) path = key_path(key2hash(key)) FileUtils.rm_f(path) end ## # Cache data # # @param [String] key # # @return [Mixed] # def cache(key) key_hash = key2hash(key) read(key_hash) || (write(key_hash, yield) if block_given?) end private ## # Convert key to hash # # @param [String] key # # @return [String] # def key2hash(key) Digest::MD5.hexdigest(key) end ## # Path from key hash # # @param [String] key_hash # # @return [String] # def key_path(key_hash) File.join(@root_path, key_hash) end ## # Read cache # # @param [String] key_hash # # @return [Mixed] # def read(key_hash) path = key_path(key_hash) Marshal.load(File.open(path)) if File.exist?(path) end ## # write cache # # @param [String] key_hash # @param [Mixed] value # # @return [Mixed] # def write(key_hash, value) return value if value.nil? || value.empty? File.open(key_path(key_hash), 'wb') do |f| Marshal.dump(value, f) end value end end |
Instance Method Details
#cache(key) ⇒ Mixed
Cache data
54 55 56 57 |
# File 'lib/gemirro/cache.rb', line 54 def cache(key) key_hash = key2hash(key) read(key_hash) || (write(key_hash, yield) if block_given?) end |
#create_root_path ⇒ Object
Create root path
25 26 27 |
# File 'lib/gemirro/cache.rb', line 25 def create_root_path FileUtils.mkdir_p(@root_path) end |
#flush ⇒ Object
Flush cache directory
32 33 34 35 |
# File 'lib/gemirro/cache.rb', line 32 def flush FileUtils.rm_rf(@root_path) create_root_path end |
#flush_key(key) ⇒ Object
Flush key
42 43 44 45 |
# File 'lib/gemirro/cache.rb', line 42 def flush_key(key) path = key_path(key2hash(key)) FileUtils.rm_f(path) end |