lib/perobs/Cache.rb in perobs-4.1.0 vs lib/perobs/Cache.rb in perobs-4.2.0

- old
+ new

@@ -1,10 +1,10 @@ # encoding: UTF-8 # # = Cache.rb -- Persistent Ruby Object Store # -# Copyright (c) 2015, 2016 by Chris Schlaeger <chris@taskjuggler.org> +# Copyright (c) 2015, 2016, 2019 by Chris Schlaeger <chris@taskjuggler.org> # # MIT License # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the @@ -64,14 +64,14 @@ # Add a PEROBS::Object to the write cache. # @param obj [PEROBS::ObjectBase] def cache_write(obj) # This is just a safety check. It can probably be disabled in the future # to increase performance. - if obj.respond_to?(:is_poxreference?) - # If this condition triggers, we have a bug in the library. - PEROBS.log.fatal "POXReference objects should never be cached" - end + #if obj.respond_to?(:is_poxreference?) + # # If this condition triggers, we have a bug in the library. + # PEROBS.log.fatal "POXReference objects should never be cached" + #end if @transaction_stack.empty? # We are not in transaction mode. idx = index(obj) if (old_obj = @writes[idx]) && old_obj._id != obj._id @@ -91,10 +91,35 @@ @transaction_objects[obj._id] = obj end end end + # Evict the object with the given ID from the cache. + # @param id [Integer] ID of the cached PEROBS::ObjectBase + # @return [True/False] True if object was stored in the cache. False + # otherwise. + def evict(id) + unless @transaction_stack.empty? + PEROBS.log.fatal "You cannot evict entries during a transaction." + end + + idx = id & @mask + # The index is just a hash. We still need to check if the object IDs are + # actually the same before we can return the object. + if (obj = @writes[idx]) && obj._id == id + # The object is in the write cache. + @writes[idx] = nil + return true + elsif (obj = @reads[idx]) && obj._id == id + # The object is in the read cache. + @reads[idx] = nil + return true + end + + false + end + # Return the PEROBS::Object with the specified ID or nil if not found. # @param id [Integer] ID of the cached PEROBS::ObjectBase def object_by_id(id) idx = id & @mask # The index is just a hash. We still need to check if the object IDs are @@ -158,10 +183,10 @@ # A nested transaction completed successfully. We add the list of # modified objects to the list of the enclosing transaction. transactions = @transaction_stack.pop # Merge the two lists @transaction_stack.push(@transaction_stack.pop + transactions) - # Ensure that each object is only included once in the list. + # Ensure that each object ID is only included once in the list. @transaction_stack.last.uniq! end end # Tell the cache to abort the currently active transaction. All modified