Class: Bits::Cache::FileCache
- Inherits:
-
Object
- Object
- Bits::Cache::FileCache
- Defined in:
- lib/bits/cache.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #bucket_for(bucket_name) ⇒ Object
- #bucket_name(string) ⇒ Object
-
#initialize(path) ⇒ FileCache
constructor
A new instance of FileCache.
- #save ⇒ Object
- #set(content) ⇒ Object
Constructor Details
#initialize(path) ⇒ FileCache
Returns a new instance of FileCache.
7 8 9 10 |
# File 'lib/bits/cache.rb', line 7 def initialize(path) @path = path @cache = Hash.new end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
5 6 7 |
# File 'lib/bits/cache.rb', line 5 def cache @cache end |
Instance Method Details
#[](key) ⇒ Object
23 24 25 26 27 |
# File 'lib/bits/cache.rb', line 23 def [](key) bucket = bucket_for(bucket_name key) return nil if bucket.nil? bucket[key] end |
#[]=(key, value) ⇒ Object
18 19 20 21 |
# File 'lib/bits/cache.rb', line 18 def []=(key, value) bucket = bucket_for(bucket_name key) bucket[key] = value end |
#bucket_for(bucket_name) ⇒ Object
12 13 14 15 16 |
# File 'lib/bits/cache.rb', line 12 def bucket_for(bucket_name) bucket = cache[bucket_name] return bucket unless bucket.nil? cache[bucket_name] = load_bucket bucket_name end |
#bucket_name(string) ⇒ Object
47 48 49 |
# File 'lib/bits/cache.rb', line 47 def bucket_name(string) string[0..1].each_byte.map { |b| sprintf("%02x",b) }.join end |
#save ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bits/cache.rb', line 35 def save FileUtils.mkdir_p @path unless File.directory? @path @cache.each do |bucket_name, bucket| path = File.join @path, "#{bucket_name}.yml" File.open path, 'w' do |f| YAML.dump bucket, f end end end |
#set(content) ⇒ Object
29 30 31 32 33 |
# File 'lib/bits/cache.rb', line 29 def set(content) content.each do |key, value| self[key] = value end end |