pinky 2.0.0-java → 2.0.1-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.0.1
data/lib/pinky/cache.rb CHANGED
@@ -2,7 +2,7 @@ module Pinky
2
2
  class Cache
3
3
  extend Forwardable
4
4
 
5
- attr_reader :name
5
+ attr_reader :name, :capacity
6
6
  def_delegators :cache, :size, :count, :empty?
7
7
 
8
8
  class << self
@@ -11,8 +11,9 @@ module Pinky
11
11
  end
12
12
  end
13
13
 
14
- def initialize(*item_methods)
15
- @item_methods = item_methods.flatten.sort
14
+ def initialize(item_methods, capacity)
15
+ @capacity = capacity
16
+ @item_methods = [item_methods].flatten.sort
16
17
  @name = self.class.name_for(@item_methods)
17
18
  end
18
19
 
@@ -25,13 +26,13 @@ module Pinky
25
26
  if action == :destroy
26
27
  cache.delete key
27
28
  else
28
- cache[key] = item
29
+ cache.put(key, item)
29
30
  end
30
31
  end
31
32
 
32
33
  def query(query_hash = {})
33
34
  key = query_hash.sort.map { |k,v| v }.join '.'
34
- cache[key]
35
+ cache.get(key)
35
36
  end
36
37
 
37
38
  def clear_cache
@@ -44,7 +45,7 @@ module Pinky
44
45
  end
45
46
 
46
47
  def cache
47
- @cache ||= {}
48
+ @cache ||= Collections.synchronizedMap(LruCache.new(@capacity));
48
49
  end
49
50
  end
50
51
  end
@@ -1,7 +1,7 @@
1
- module Pinky
1
+ module Pinky
2
2
  module HasCaches
3
- def cachable_by(*methods)
4
- create_cache_for methods
3
+ def cachable_by(opts)
4
+ create_cache_for opts[:method_names], opts[:capacity]
5
5
  end
6
6
 
7
7
  def update_caches_with(item_hash, action = :create)
@@ -15,8 +15,8 @@ module Pinky
15
15
  end
16
16
 
17
17
  private
18
- def create_cache_for(method_names)
19
- cache = Cache.new method_names
18
+ def create_cache_for(method_names, capacity)
19
+ cache = Cache.new(method_names, capacity || 100)
20
20
  pinky_caches_by_name[cache.name] = cache
21
21
  end
22
22
 
@@ -27,7 +27,5 @@ module Pinky
27
27
  def pinky_caches
28
28
  pinky_caches_by_name.values
29
29
  end
30
-
31
30
  end
32
-
33
31
  end
@@ -0,0 +1,17 @@
1
+ java_import java.util.LinkedHashMap
2
+ java_import java.util.Collections
3
+
4
+ module Pinky
5
+ class LruCache < LinkedHashMap
6
+ attr_reader :maxEntries
7
+
8
+ def initialize(maxEntries)
9
+ super(maxEntries + 1, 1, true)
10
+ @maxEntries = maxEntries;
11
+ end
12
+
13
+ def removeEldestEntry(map)
14
+ return true if size > @maxEntries
15
+ end
16
+ end
17
+ end
data/pinky.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "pinky"
8
- s.version = "2.0.0"
8
+ s.version = "2.0.1"
9
9
  s.platform = "java"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  "lib/pinky/cache.rb",
32
32
  "lib/pinky/exceptions.rb",
33
33
  "lib/pinky/has_caches.rb",
34
+ "lib/pinky/lru_cache.rb",
34
35
  "lib/pinky/model.rb",
35
36
  "lib/pinky/model_fetch_methods.rb",
36
37
  "pinky.gemspec",
@@ -3,7 +3,7 @@ require 'date'
3
3
 
4
4
  module Pinky
5
5
  describe Cache do
6
- let(:cache) { Cache.new :id, :name }
6
+ let(:cache) { Cache.new([:id, :name], 100) }
7
7
 
8
8
  context 'Cache#name_for' do
9
9
  it { Cache.name_for(:id).should == 'cache_by_id' }
@@ -36,6 +36,22 @@ module Pinky
36
36
  cache.update_with(item, :destroy)
37
37
  cache.empty?.should be_true
38
38
  end
39
+
40
+ it 'should only store 20 items if capacity is capped at 20' do
41
+ cache = Cache.new([:id, :name], 2)
42
+ 10.times {|i| cache.update_with(mock(id: i, name: 'foo'), :create) }
43
+ cache.send(:cache).size.should == 2
44
+ end
45
+
46
+ it 'orders most recently used items ordered by usage' do
47
+ cache = Cache.new([:id, :name], 5)
48
+ 10.times do |i|
49
+ cache.update_with(mock(id: i, name: 'foo'), :create)
50
+ cache.query(:id => 5, :name => 'foo')
51
+ cache.query(:id => 7, :name => 'foo')
52
+ end
53
+ cache.send(:cache).keys.last(2).should == ['5.foo', '7.foo']
54
+ end
39
55
  end
40
56
 
41
57
  context 'queryable' do
@@ -5,8 +5,8 @@ module Pinky
5
5
  context 'with cachable_by declared' do
6
6
  klass = Class.new do
7
7
  extend Pinky::HasCaches
8
- cachable_by :token
9
- cachable_by :id, :foo
8
+ cachable_by method_names: :token
9
+ cachable_by method_names: [:id, :foo]
10
10
 
11
11
  attr_reader :id, :foo, :bar, :token
12
12
  def initialize id, foo, bar, token
@@ -5,7 +5,7 @@ module Pinky
5
5
  context 'caches' do
6
6
  member_klass = Class.new do
7
7
  extend Pinky::ModelFetchMethods
8
- cachable_by :id
8
+ cachable_by method_names: :id
9
9
 
10
10
  def initialize hash; @hash = hash end
11
11
  def id; @hash[:id] end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: pinky
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 2.0.0
5
+ version: 2.0.1
6
6
  platform: java
7
7
  authors:
8
8
  - Joel Friedman
@@ -152,6 +152,7 @@ files:
152
152
  - lib/pinky/cache.rb
153
153
  - lib/pinky/exceptions.rb
154
154
  - lib/pinky/has_caches.rb
155
+ - lib/pinky/lru_cache.rb
155
156
  - lib/pinky/model.rb
156
157
  - lib/pinky/model_fetch_methods.rb
157
158
  - pinky.gemspec