level2 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0da09ce71772bfe62d6e3b123a66b0f308b973f9
4
- data.tar.gz: 97ae48d6a7e1583ed4496503b28979ee9a8ec416
3
+ metadata.gz: 89ba62e1f21baef8ee3a80060af7097b77ac6d98
4
+ data.tar.gz: 24b666e5ceeaacf63769b2928483afc83567e79e
5
5
  SHA512:
6
- metadata.gz: c7016bd999b92dbbc226db2c8f46ad4e0805d3325b42c6a02b3326261619440ebbba1f2b9dc7d885654355e8b8b28a514b25e7021d435ec86aae2f40795a72e5
7
- data.tar.gz: 3fb8c7b2c4690376a1769d27d590dcf4bfcbfee40433a48e25313d7dc4fb53e703c3f722cc9cb87e484b938cdca075e9bdd96ce1b5e7faa9cc29090ca57f3b48
6
+ metadata.gz: 6e9274d7574970ff0542c3ec84cb524680f08feb1c1da7fb404faa0897e8b1c5b2b3e144ddccd0a9317a75bcccdb111ed5274f4cde00f8b3da2a0fdd274bbaa9
7
+ data.tar.gz: 4753d3f1e2a4b77e4b8a9523aaf87627393b0289c245570e39981a432ee424853f28bf4b5cb9a07286159e9e0da328eececc85ae83cbc8843638d38eb55f261f
data/README.md CHANGED
@@ -50,14 +50,35 @@ Example:
50
50
  ```ruby
51
51
  # in config/application.rb
52
52
 
53
- config.cache_store = :level2, [
54
- :memory_store, size: 32.megabytes
55
- ],[
56
- :mem_cache_store, 'host1.example.org:11211', 'host2.example.org:11211'
57
- ]
53
+ config.cache_store = :level2,
54
+ L1: [
55
+ :memory_store, size: 32.megabytes
56
+ ],
57
+ L2: [
58
+ :mem_cache_store, 'host1.example.org:11211'
59
+ ]
58
60
  ```
59
61
 
62
+ ## Notifications
60
63
 
64
+ Level2 enriches
65
+ [`ActiveSupport::Notifications`](http://edgeguides.rubyonrails.org/active_support_instrumentation.html#active-support).
66
+
67
+ Event payloads will include a `:level` field. On cache hits, this will indicate
68
+ where the hit comes from; on misses, or any other event, the field may be
69
+ present but the value is unspecified.
70
+
71
+ Example:
72
+
73
+ ```ruby
74
+ # in an initializer
75
+ ActiveSupport::Notifications.subscribe 'cache_read.active_support' do |*args|
76
+ event = ActiveSupport::Notifications::Event.new(*args)
77
+ if event.payload[:hit]
78
+ Rails.logger.info "Hit from #{event.payload[:level]}"
79
+ end
80
+ end
81
+ ```
61
82
 
62
83
  ## Development
63
84
 
data/level2.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'level2'
7
- spec.version = '0.1.0'
7
+ spec.version = '0.2.0'
8
8
  spec.authors = ['Julien Letessier']
9
9
  spec.email = ['julien.letessier@gmail.com']
10
10
 
@@ -5,10 +5,10 @@ module ActiveSupport
5
5
  class Level2 < Store
6
6
  attr_reader :stores
7
7
 
8
- def initialize(*store_options)
8
+ def initialize(store_options)
9
9
  @lock = Mutex.new
10
- @stores = store_options.each_slice(2).map do |name,options|
11
- ActiveSupport::Cache.lookup_store(name, options)
10
+ @stores = store_options.each_with_object({}) do |(name,options), h|
11
+ h[name] = ActiveSupport::Cache.lookup_store(options)
12
12
  end
13
13
  @options = {}
14
14
  end
@@ -27,6 +27,13 @@ module ActiveSupport
27
27
 
28
28
  protected
29
29
 
30
+ def instrument(operation, key, options = nil, &block)
31
+ super(operation, key, options) do |payload|
32
+ payload[:level] = current_level
33
+ block.call(payload)
34
+ end
35
+ end
36
+
30
37
  def read_entry(key, options)
31
38
  @lock.synchronize do
32
39
  read_entry_from(@stores, key, options)
@@ -35,7 +42,7 @@ module ActiveSupport
35
42
 
36
43
  def write_entry(key, entry, options)
37
44
  @lock.synchronize do
38
- @stores.each do |store|
45
+ @stores.each do |name, store|
39
46
  result = store.send :write_entry, key, entry, options
40
47
  return false unless result
41
48
  end
@@ -45,7 +52,7 @@ module ActiveSupport
45
52
 
46
53
  def delete_entry(key, options)
47
54
  @lock.synchronize do
48
- @stores.map { |store|
55
+ @stores.map { |name,store|
49
56
  store.send :delete_entry, key, options
50
57
  }.all?
51
58
  end
@@ -53,10 +60,19 @@ module ActiveSupport
53
60
 
54
61
  private
55
62
 
63
+ def current_level
64
+ Thread.current[:level2_current]
65
+ end
66
+
67
+ def current_level!(name)
68
+ Thread.current[:level2_current] = name
69
+ end
70
+
56
71
  def read_entry_from(stores, key, options)
57
72
  return if stores.empty?
58
73
 
59
- store, *other_stores = stores
74
+ (name,store), *other_stores = stores.to_a
75
+ current_level! name
60
76
  entry = store.send :read_entry, key, options
61
77
  return entry if entry.present?
62
78
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: level2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Letessier
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-06 00:00:00.000000000 Z
11
+ date: 2016-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler