Class: Bucketer::InMemory
- Inherits:
-
Object
- Object
- Bucketer::InMemory
- Defined in:
- lib/bucketer/in_memory.rb
Overview
This is a purpose built class for storing arbitrary objects in buckets then calling callbacks when any of those buckets exceed a certain pre-specified size. The interface is intentially evented looking so that it would be possible to later swap out the backing database with a more persistent database (eg. redis). If this interface was using regular returns this would mean clients would need to change later if we decided to use redis.
Constant Summary collapse
- BUCKET_THRESHOLD_SIZE_DEFAULT =
1000- BUCKET_MAX_AGE_DEFAULT =
3600
Instance Method Summary collapse
-
#add_item(bucket_id, item_id, item, &blk) ⇒ Object
Adds a item to the specified bucket and calls the block when it is done.
-
#empty_bucket(bucket_id, &blk) ⇒ Object
Empty a bucket.
-
#get_and_empty_bucket(bucket_id) {|Array| ... } ⇒ Object
Get the contents of a bucket then empty it.
-
#get_bucket(bucket_id) {|Array| ... } ⇒ Object
Get the contents of a bucket.
-
#initialize(bucket_threshold_size: BUCKET_THRESHOLD_SIZE_DEFAULT, bucket_max_age: BUCKET_MAX_AGE_DEFAULT) ⇒ InMemory
constructor
Creates a new in memory Bucketer with the requested configurations.
-
#on_bucket_full {|String| ... } ⇒ Object
Used to set a callback hook for when a bucket reaches the threshold size.
Constructor Details
#initialize(bucket_threshold_size: BUCKET_THRESHOLD_SIZE_DEFAULT, bucket_max_age: BUCKET_MAX_AGE_DEFAULT) ⇒ InMemory
Creates a new in memory Bucketer with the requested configurations
after which the on_bucket_full callback is called can remain before the on_bucket_timed_out is called
22 23 24 25 26 27 |
# File 'lib/bucketer/in_memory.rb', line 22 def initialize(bucket_threshold_size: BUCKET_THRESHOLD_SIZE_DEFAULT, bucket_max_age: BUCKET_MAX_AGE_DEFAULT) @bucket_threshold_size = bucket_threshold_size @bucket_max_age = bucket_max_age @buckets = {} @on_bucket_full_callbacks = [] end |
Instance Method Details
#add_item(bucket_id, item_id, item, &blk) ⇒ Object
Adds a item to the specified bucket and calls the block when it is done
the bucket to put the item in of the item (used to ensure uniqueness within a bucket) placed in the bucket
39 40 41 42 |
# File 'lib/bucketer/in_memory.rb', line 39 def add_item(bucket_id, item_id, item, &blk) add_bucket_to_db(bucket_id, item_id, item, &blk) check_bucket_full(bucket_id) end |
#empty_bucket(bucket_id, &blk) ⇒ Object
Empty a bucket
of the bucket you want to empty
87 88 89 90 91 |
# File 'lib/bucketer/in_memory.rb', line 87 def empty_bucket(bucket_id, &blk) empty_bucket_in_db(bucket_id) do blk.call if block_given? end end |
#get_and_empty_bucket(bucket_id) {|Array| ... } ⇒ Object
Get the contents of a bucket then empty it
of the bucket you want to get into the bucket
75 76 77 78 79 80 81 |
# File 'lib/bucketer/in_memory.rb', line 75 def get_and_empty_bucket(bucket_id, &blk) get_bucket(bucket_id) do |contents| empty_bucket(bucket_id) do blk.call contents end end end |
#get_bucket(bucket_id) {|Array| ... } ⇒ Object
Get the contents of a bucket.
of the bucket you want to get into the bucket
63 64 65 66 67 |
# File 'lib/bucketer/in_memory.rb', line 63 def get_bucket(bucket_id, &blk) get_bucket_from_db(bucket_id) do |bucket| blk.call bucket.values end end |
#on_bucket_full {|String| ... } ⇒ Object
Used to set a callback hook for when a bucket reaches the threshold size. It is IMPORTANT to note that the bucket will not automatically be emptied you must call empty_bucket if you want the bucket to be emptied. Also the callback will be called every time a item is added until the bucket is emptied.
53 54 55 |
# File 'lib/bucketer/in_memory.rb', line 53 def on_bucket_full(&blk) @on_bucket_full_callbacks << blk end |