Class: Tango::Resource::Cache
- Inherits:
-
Object
- Object
- Tango::Resource::Cache
- Defined in:
- lib/tango/resource/cache.rb
Overview
Key - value caching system for resources
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
Instance Method Summary collapse
-
#get(type, resource) ⇒ Object
Getter for the cache storage.
-
#initialize(buffer = nil) ⇒ Tango::Resources::Cache
constructor
Constructor of the Cache.
-
#load(type, resource) ⇒ Object
Get a cached resource or use given block to obtain resource and return it.
-
#register(type, &release_callback) ⇒ Tango::Resource::Cache
Register new type of resource to be cached.
-
#set(type, resource) ⇒ Object
Setter for the cache storage.
Constructor Details
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
9 10 11 |
# File 'lib/tango/resource/cache.rb', line 9 def buffer @buffer end |
Instance Method Details
#get(type, resource) ⇒ Object
Getter for the cache storage
80 81 82 83 |
# File 'lib/tango/resource/cache.rb', line 80 def get( type, resource ) raise ArgumentError, "Trying to get resource with unregistered type" unless @storage.keys.include?( type ) @storage[type][resource.cache_key] end |
#load(type, resource) ⇒ Object
Get a cached resource or use given block to obtain resource and return it
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tango/resource/cache.rb', line 43 def load( type, resource ) # Get resource from cache cached_resource = get( type, resource ) unless cached_resource raise ArgumentError, "No resource callback given" unless block_given? # If not found, execute yield to receive transformed resource cached_resource = yield( resource ) # Cache new resource set( type, cached_resource ) # Fill buffer with newly cached resource @buffer.fill( type, cached_resource ) end cached_resource end |
#register(type, &release_callback) ⇒ Tango::Resource::Cache
Register new type of resource to be cached
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/tango/resource/cache.rb', line 27 def register( type, &release_callback ) # Create container for cache of new resource @storage[type] = {} # Also register new type with buffer @buffer.register( type, &release_callback ) self end |
#set(type, resource) ⇒ Object
Setter for the cache storage
70 71 72 73 |
# File 'lib/tango/resource/cache.rb', line 70 def set( type, resource ) raise ArgumentError, "Trying to set resource with unregistered type" unless @storage.keys.include?( type ) @storage[type][resource.cache_key] = resource end |