Class: Rotary::Pool
- Inherits:
-
Object
- Object
- Rotary::Pool
- Defined in:
- lib/rotary.rb
Instance Method Summary collapse
-
#clean_older_than(n, &block) ⇒ Object
Removes all elements from pool, which means specified by block argument criteria.
- #clear ⇒ Object
- #get ⇒ Object
-
#initialize(storage: STORAGE, connection: nil, serializer: SERIALIZER, limit: LIMIT, ttl: TTL, prefix: PREFIX, on_overflow: ->(obj) { raise OverflowError }, create: create) ⇒ Pool
constructor
A new instance of Pool.
- #set(obj) ⇒ Object
- #size ⇒ Object
-
#with ⇒ Object
block should return object, because object could’ve been mutated.
Constructor Details
#initialize(storage: STORAGE, connection: nil, serializer: SERIALIZER, limit: LIMIT, ttl: TTL, prefix: PREFIX, on_overflow: ->(obj) { raise OverflowError }, create: create) ⇒ Pool
Returns a new instance of Pool.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rotary.rb', line 19 def initialize( storage: STORAGE, connection: nil, serializer: SERIALIZER, limit: LIMIT, ttl: TTL, prefix: PREFIX, on_overflow: ->(obj) { raise OverflowError }, create: create ) @limit = limit = connection ? { connection: connection } : {} @create_obj = create @on_overflow = on_overflow storage = Rotary::Storage.load_storage(storage) @serializer = Rotary::Serializer.load_serializer(serializer) @storage = storage.new( connection: connection || storage.default_connection, ttl: ttl, serializer: @serializer, prefix: prefix ) end |
Instance Method Details
#clean_older_than(n, &block) ⇒ Object
Removes all elements from pool, which means specified by block argument criteria.
69 70 71 72 73 74 75 |
# File 'lib/rotary.rb', line 69 def clean_older_than(n, &block) if @storage.respond_to?(:clean_older_than) @storage.clean_older_than(n, &block) else fail "#{@storage.class}#clean_where not implemented" end end |
#clear ⇒ Object
58 59 60 |
# File 'lib/rotary.rb', line 58 def clear @storage.clear end |
#get ⇒ Object
43 44 45 |
# File 'lib/rotary.rb', line 43 def get @storage.pop || @create_obj.call end |
#set(obj) ⇒ Object
62 63 64 65 |
# File 'lib/rotary.rb', line 62 def set(obj) return @on_overflow.call(obj) if @limit && @storage.size >= @limit @storage.push(obj) end |
#size ⇒ Object
54 55 56 |
# File 'lib/rotary.rb', line 54 def size @storage.size end |
#with ⇒ Object
block should return object, because object could’ve been mutated
48 49 50 51 52 |
# File 'lib/rotary.rb', line 48 def with object = get object = yield(object) set(object) end |