Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,3 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in truc.gemspec
gemspec

group :test do
gem 'hashery', '~> 2.1.1'
gem "yajl-ruby", require: 'yajl', platforms: [:ruby_19, :ruby_20, :ruby_21]
end

51 changes: 23 additions & 28 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,36 @@ PATH
remote: .
specs:
prismic.io (1.8.2)
hashery (~> 2.1.1)

GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.5.0)
hashery (2.1.2)
mini_portile2 (2.8.2)
nokogiri (1.15.2)
diff-lcs (1.6.2)
mini_portile2 (2.8.9)
nokogiri (1.19.3)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.15.2-java)
nokogiri (1.19.3-java)
racc (~> 1.4)
nokogiri (1.15.2-x86_64-darwin)
nokogiri (1.19.3-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.15.2-x86_64-linux)
nokogiri (1.19.3-x86_64-linux-gnu)
racc (~> 1.4)
racc (1.7.0)
racc (1.7.0-java)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
racc (1.8.1)
racc (1.8.1-java)
rspec (3.13.2)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.6)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.5)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.8)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
yajl-ruby (1.4.1)
rspec-support (~> 3.13.0)
rspec-support (3.13.7)

PLATFORMS
java
Expand All @@ -43,12 +40,10 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
hashery (~> 2.1.1)
nokogiri (~> 1.6)
nokogiri
prismic.io!
rspec (~> 3.12)
rspec-core (~> 3.12)
yajl-ruby
rspec
rspec-core

BUNDLED WITH
2.2.20
4.0.12
48 changes: 22 additions & 26 deletions lib/prismic/cache/lru.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# encoding: utf-8

require 'hashery'

module Prismic
# This is a simple cache class provided with the prismic.io Ruby kit.
#
Expand All @@ -14,13 +10,12 @@ module Prismic
# you can create your API object like this: `Prismic.api(url, cache:
# Prismic::DefaultCache)`
class LruCache

# @return [LRUHash<String,Object>]
attr_reader :intern

# @param max_size [Fixnum] (100) The default maximum of keys to store
def initialize(max_size=100)
@intern = Hashery::LRUHash.new(max_size)
def initialize(max_size = 100)
@max_size = max_size
@intern = {}
end

# Add a cache entry.
Expand All @@ -30,7 +25,9 @@ def initialize(max_size=100)
#
# @return [Object] The stored value
def set(key, value, expired_in = nil)
@intern.store(key, { :data => value, :expired_in => expired_in = expired_in && Time.now.getutc.to_i + expired_in })
@intern.delete(key)
@intern[key] = { data: value, expired_in: expired_in && Time.now.getutc.to_i + expired_in }
@intern.delete(@intern.keys.first) while @intern.size > @max_size
value
end

Expand All @@ -45,16 +42,18 @@ def []=(key, value)
# @return [Object] The cache object as was stored
def get(key)
return delete(key) if expired?(key)
include?(key) ? @intern[key][:data] : nil
return nil unless include?(key)

entry = @intern.delete(key)
@intern[key] = entry
entry[:data]
end
alias :[] :get
alias [] get

def get_or_set(key, value = nil, expired_in = nil)
if include?(key) && !expired?(key)
return get(key)
else
set(key, block_given? ? yield : value, expired_in)
end
return get(key) if include?(key) && !expired?(key)

set(key, block_given? ? yield : value, expired_in)
end

def delete(key)
Expand All @@ -70,10 +69,10 @@ def delete(key)
def has_key?(key)
@intern.has_key?(key)
end
alias :include? :has_key?
alias include? has_key?

def expired?(key)
if include?(key) && @intern[key][:expired_in] != nil
if include?(key) && !@intern[key][:expired_in].nil?
expired_in = @intern[key][:expired_in]
expired_in && expired_in < Time.now.getutc.to_i
else
Expand All @@ -85,7 +84,7 @@ def expired?(key)
def invalidate_all!
@intern.clear
end
alias :clear! :invalidate_all!
alias clear! invalidate_all!

# Expose the Hash keys
#
Expand All @@ -102,20 +101,17 @@ def keys
def size
@intern.size
end
alias :length :size

alias length size
end

# Available as an api cache for testing purposes (no caching)
class BasicNullCache
def get(key)
end
def get(key); end

def set(key, value = nil, expired_in = nil)
def set(_key, value = nil, _expired_in = nil)
block_given? ? yield : value
end
alias_method :get_or_set, :set

alias get_or_set set
end

# This default instance is used by the API to avoid creating a new instance
Expand Down
Loading