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
24 25 26 27 28 29 |
# File 'lib/bucketer/in_memory.rb', line 24 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
41 42 43 44 45 46 47 |
# File 'lib/bucketer/in_memory.rb', line 41 def add_item(bucket_id, item_id, item, &blk) EM::Completion.new.tap do |c| c.callback(&blk) if block_given? add_bucket_to_db(bucket_id, item_id, item) { c.succeed } check_bucket_full(bucket_id) end end |
#empty_bucket(bucket_id, &blk) ⇒ Object
Empty a bucket
of the bucket you want to empty
98 99 100 101 102 103 104 105 |
# File 'lib/bucketer/in_memory.rb', line 98 def empty_bucket(bucket_id, &blk) EM::Completion.new.tap do |c| c.callback(&blk) if block_given? empty_bucket_in_db(bucket_id) do c.succeed end 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
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bucketer/in_memory.rb', line 83 def get_and_empty_bucket(bucket_id, &blk) EM::Completion.new.tap do |c| c.callback(&blk) if block_given? get_bucket(bucket_id) do |contents| empty_bucket(bucket_id) do c.succeed contents end 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
68 69 70 71 72 73 74 75 |
# File 'lib/bucketer/in_memory.rb', line 68 def get_bucket(bucket_id, &blk) EM::Completion.new.tap do |c| c.callback(&blk) if block_given? get_bucket_from_db(bucket_id) do |bucket| c.succeed bucket.values end 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.
58 59 60 |
# File 'lib/bucketer/in_memory.rb', line 58 def on_bucket_full(&blk) @on_bucket_full_callbacks << blk end |