cached_key_value_store 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # CachedKeyValueStore
2
2
 
3
- TODO: Write a gem description
3
+ Sending redis a request for each of those thousands of translations scattered
4
+ throughout your app is slow. This gem memoizes those requests, and sets up a
5
+ simple mechanism to bust the cache.
6
+
7
+ If you want to use Redis for I18n, I recommend you watch
8
+ [this railscast](http://railscasts.com/episodes/256-i18n-backends),
9
+ and use this backend instead of the KeyValue one that he uses.
4
10
 
5
11
  ## Installation
6
12
 
@@ -18,7 +24,30 @@ Or install it yourself as:
18
24
 
19
25
  ## Usage
20
26
 
21
- TODO: Write usage instructions here
27
+ In your initializer:
28
+
29
+ I18n.backend = I18n::Backend::CachedKeyValueStore.new($redis)
30
+
31
+ ### Busting the Cache
32
+
33
+ The ```#ensure_freshness!``` method can be called periodically to make sure
34
+ that new changes show up. I put mine in a before filter:
35
+
36
+ class ApplicationController < ActionController::Base
37
+ before_filter :ensure_fresh_i18n
38
+
39
+ private
40
+ def ensure_fresh_i18n
41
+ I18n.backend.ensure_freshness! I18n.locale
42
+ end
43
+ end
44
+
45
+ You can also call ```#update_version!(locale)``` yourself to signal that the
46
+ translations have been modified.
47
+
48
+ The ```on_update_version``` hook will be triggered every time the version is
49
+ updated.
50
+
22
51
 
23
52
  ## Contributing
24
53
 
@@ -5,9 +5,9 @@ Gem::Specification.new do |s|
5
5
  s.name = 'cached_key_value_store'
6
6
  s.version = CachedKeyValueStore::VERSION
7
7
  s.summary = 'An I18n Backend with in-memory cacheing'
8
- s.description = %q{This gem is designed for usage with a key-value store
9
- database, where latency is an issue. Locale changes are
10
- versioned to allow for an easy cache-bust. }
8
+ s.description = %q{This gem is designed for usage with a I18n backend
9
+ key-value database, where latency is an issue. I18n
10
+ changes are versioned to allow for an easy cache-bust.}
11
11
  s.authors = ['Mike Wheeler']
12
12
  s.email = 'mwheeler@g2crowd.com'
13
13
 
@@ -15,7 +15,7 @@ Gem::Specification.new do |s|
15
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
- s.homepage = 'http://rubygems.org/gems/cached-key-value-store'
18
+ s.homepage = 'https://github.com/wheeyls/cached_key_value_store'
19
19
 
20
20
  s.add_dependency "activesupport"
21
21
  s.add_development_dependency "rspec", "~> 2.6"
@@ -1,3 +1,3 @@
1
1
  module CachedKeyValueStore
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -25,11 +25,20 @@ module I18n
25
25
 
26
26
  def update_version!(locale)
27
27
  @store["#{KEY_PREFIX}#{locale}"] = Time.now.to_i
28
+ on_update_version locale
28
29
  end
29
30
 
30
31
  def current_version(locale)
31
32
  @store["#{KEY_PREFIX}#{locale}"]
32
33
  end
34
+
35
+ def on_update_version(locale = nil, &block)
36
+ if block_given?
37
+ @on_store_block = block
38
+ elsif @on_store_block != nil
39
+ @on_store_block.call locale
40
+ end
41
+ end
33
42
  end
34
43
  end
35
44
  end
@@ -51,4 +51,46 @@ describe I18n::Backend::CachedKeyValueStore do
51
51
  store['en.simple'].should match(/test/)
52
52
  end
53
53
  end
54
+
55
+ describe '#update_version!' do
56
+ it 'writes a timestamp to the store' do
57
+ Time.should_receive(:now).and_return '123'
58
+ key = 'i18n:locale_version:en'
59
+
60
+ subject.update_version!(:en)
61
+ store[key].should == 123
62
+ end
63
+
64
+ it 'triggers a hook' do
65
+ subject.should_receive(:on_update_version).with :en
66
+ subject.update_version!(:en)
67
+ end
68
+ end
69
+
70
+ describe '#on_update_version' do
71
+ context 'with a block' do
72
+ before do
73
+ @prc = Proc.new { }
74
+ subject.on_update_version &@prc
75
+ end
76
+
77
+ it 'stores a block you pass to it' do
78
+ @prc.should_not_receive(:call)
79
+ subject.on_update_version { }
80
+ end
81
+
82
+ it 'triggers the block if no block is passed' do
83
+ @prc.should_receive(:call).with :en
84
+ subject.on_update_version :en
85
+ end
86
+ end
87
+
88
+ context 'without a block' do
89
+ it 'does nothing if no block is passed' do
90
+ expect {
91
+ subject.on_update_version
92
+ }.to_not raise_error
93
+ end
94
+ end
95
+ end
54
96
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cached_key_value_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-26 00:00:00.000000000 Z
12
+ date: 2013-06-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -43,9 +43,9 @@ dependencies:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
45
  version: '2.6'
46
- description: ! "This gem is designed for usage with a key-value store\n database,
47
- where latency is an issue. Locale changes are\n versioned
48
- to allow for an easy cache-bust. "
46
+ description: ! "This gem is designed for usage with a I18n backend\n key-value
47
+ database, where latency is an issue. I18n\n changes are versioned
48
+ to allow for an easy cache-bust."
49
49
  email: mwheeler@g2crowd.com
50
50
  executables: []
51
51
  extensions: []
@@ -63,7 +63,7 @@ files:
63
63
  - lib/i18n/backend/cached_key_value_store.rb
64
64
  - spec/i18n/backend/cached_key_value_store_spec.rb
65
65
  - spec/spec_helper.rb
66
- homepage: http://rubygems.org/gems/cached-key-value-store
66
+ homepage: https://github.com/wheeyls/cached_key_value_store
67
67
  licenses: []
68
68
  post_install_message:
69
69
  rdoc_options: []