Class: Lafcadio::ObjectStore::Cache
- Inherits:
-
Object
- Object
- Lafcadio::ObjectStore::Cache
- Defined in:
- lib/lafcadio/objectStore/Cache.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#flush(dbObject) ⇒ Object
Flushes a domain object.
- #flush_collection_cache(objectType) ⇒ Object
-
#get(objectType, pkId) ⇒ Object
Returns a cached domain object, or nil if none is found.
-
#getAll(objectType) ⇒ Object
Returns an array of all domain objects of a given type.
- #getByQuery(query) ⇒ Object
- #hashByObjectType(objectType) ⇒ Object
-
#initialize(dbBridge) ⇒ Cache
constructor
A new instance of Cache.
- #last_commit_time(domain_class, pkId) ⇒ Object
-
#save(dbObject) ⇒ Object
Saves a domain object.
- #set_commit_time(d_obj) ⇒ Object
Constructor Details
#initialize(dbBridge) ⇒ Cache
Returns a new instance of Cache.
6 7 8 9 10 11 |
# File 'lib/lafcadio/objectStore/Cache.rb', line 6 def initialize( dbBridge ) @dbBridge = dbBridge @objects = {} @collections_by_query = {} @commit_times = {} end |
Instance Method Details
#flush(dbObject) ⇒ Object
Flushes a domain object.
53 54 55 56 |
# File 'lib/lafcadio/objectStore/Cache.rb', line 53 def flush(dbObject) hashByObjectType(dbObject.objectType).delete dbObject.pkId flush_collection_cache( dbObject.objectType ) end |
#flush_collection_cache(objectType) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/lafcadio/objectStore/Cache.rb', line 58 def flush_collection_cache( objectType ) @collections_by_query.keys.each { |query| if query.objectType == objectType @collections_by_query.delete( query ) end } end |
#get(objectType, pkId) ⇒ Object
Returns a cached domain object, or nil if none is found.
21 22 23 |
# File 'lib/lafcadio/objectStore/Cache.rb', line 21 def get(objectType, pkId) hashByObjectType(objectType)[pkId].clone end |
#getAll(objectType) ⇒ Object
Returns an array of all domain objects of a given type.
48 49 50 |
# File 'lib/lafcadio/objectStore/Cache.rb', line 48 def getAll(objectType) hashByObjectType(objectType).values.collect { |d_obj| d_obj.clone } end |
#getByQuery(query) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/lafcadio/objectStore/Cache.rb', line 31 def getByQuery( query ) unless @collections_by_query[query] newObjects = @dbBridge.getCollectionByQuery(query) newObjects.each { |dbObj| save dbObj } @collections_by_query[query] = newObjects.collect { |dobj| dobj.pkId } end collection = [] @collections_by_query[query].each { |pkId| dobj = get( query.objectType, pkId ) collection << dobj if dobj } collection end |
#hashByObjectType(objectType) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/lafcadio/objectStore/Cache.rb', line 13 def hashByObjectType(objectType) unless @objects[objectType] @objects[objectType] = {} end @objects[objectType] end |
#last_commit_time(domain_class, pkId) ⇒ Object
66 67 68 69 |
# File 'lib/lafcadio/objectStore/Cache.rb', line 66 def last_commit_time( domain_class, pkId ) by_domain_class = @commit_times[domain_class] by_domain_class ? by_domain_class[pkId] : nil end |
#save(dbObject) ⇒ Object
Saves a domain object.
26 27 28 29 |
# File 'lib/lafcadio/objectStore/Cache.rb', line 26 def save(dbObject) hashByObjectType(dbObject.objectType)[dbObject.pkId] = dbObject flush_collection_cache( dbObject.objectType ) end |
#set_commit_time(d_obj) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/lafcadio/objectStore/Cache.rb', line 71 def set_commit_time( d_obj ) by_domain_class = @commit_times[d_obj.objectType] if by_domain_class.nil? by_domain_class = {} @commit_times[d_obj.objectType] = by_domain_class end by_domain_class[d_obj.pkId] = Time.now end |