resterl 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NTEyZGFhZmRlNTc1ZTQxZmVhODliZTYxNjkzYTExMDA2OGFhYzExNA==
5
- data.tar.gz: !binary |-
6
- ZTBiY2I0NTJlNzFhZjcxZGMxYTA0Nzc3ODZjYzQ3MTQxNjA3OTNhYg==
2
+ SHA1:
3
+ metadata.gz: e927695ca56a351455ca5ea51eab5201f4663ba3
4
+ data.tar.gz: 24d36643d33e40fe72f08496b27874bb8cccb1bd
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- MDRiOWMwYTgyNWU2MWUyOTEwZDRjYjgwMjhjNmRkNThhZmMzZTY0MWNlYzE5
10
- ZjkwODhhOGRhOTQyMmIwYjU3OTJiYzAzYzgxODE4ZjRkOGM3MzE5ZmRmNmFj
11
- Yzg0MTcwNTZlNDBkZmEyZmExMmI0NDMxNzI0YjRlOGIzYTY2OGQ=
12
- data.tar.gz: !binary |-
13
- ZmYxOTg1NWYwZGY2OGI2MTE4ZGYzNjE4NTNjZmU0ODU1M2YwZGRhYzZkMDA5
14
- MTQ2ZGNkM2IwZGEzZTgzZTk4ZjkyZTQ4Nzk5MGQyMDVlNzc5MWNlYTU0MzRm
15
- NTBkZGVjMTY1NzBhZDRkNzJlNThiMjE5ZGJkYTBiZDFhYmI0YzQ=
6
+ metadata.gz: d3c59e6dd212727d2c538d5fe1cc530eef15d45303c385c8f273f0df8977d1e8800022eb760aaeb399e0dc20454f5c581fecb1a327f07c77d26339d2015bad88
7
+ data.tar.gz: 34313e88f25883f86d235fca6c5467a0b124c3d07f4493643e1941ae7e553510ae314cfd0f36d120bc7dd3341f16b35e7084d91dded394176ddbf0083be39006
@@ -1,109 +1,113 @@
1
1
  # From:
2
2
  # http://github.com/der-flo/localmemcache_store/blob/master/lib/expiry_cache.rb
3
3
 
4
- class Resterl::Caches::SimpleCache < Resterl::Caches::CacheInterface
5
-
6
- class Entry < Struct.new(:data, :expires_at)
7
- def expired?
8
- expires_at && expires_at <= Time.now
9
- end
10
- end
11
-
12
- def initialize options = {}
13
- @options = {
14
- :expiration_check_interval => 1_000,
15
- :cache_key_prefix => 'RESTERL_'
16
- }.merge options
17
- @expiration_check_counter = 0
18
- @data = {}
19
- end
20
-
21
- def read key
22
- key = internal_key key
23
- #ap "in read #{key}"
24
-
25
- do_expiration_check
26
- entry = @data[key]
27
-
28
- # return if nothing in cache
29
- return nil unless entry
30
-
31
- # entry expired?
32
- if verify_entry_not_expired(key, entry)
33
- entry.data
34
- else
35
- nil
4
+ module Resterl
5
+ module Caches
6
+ class SimpleCache < CacheInterface
7
+ class Entry < Struct.new(:data, :expires_at)
8
+ def expired?
9
+ expires_at && expires_at <= Time.now
10
+ end
11
+ end
12
+
13
+ def initialize options = {}
14
+ @options = {
15
+ expiration_check_interval: 1_000,
16
+ cache_key_prefix: 'RESTERL_'
17
+ }.merge options
18
+ @expiration_check_counter = 0
19
+ @data = {}
20
+ end
21
+
22
+ def read key
23
+ key = internal_key key
24
+
25
+ do_expiration_check
26
+ entry = @data[key]
27
+
28
+ # return if nothing in cache
29
+ return nil unless entry
30
+
31
+ # entry expired?
32
+ if verify_entry_not_expired(key, entry)
33
+ entry.data
34
+ else
35
+ nil
36
+ end
37
+ end
38
+
39
+ def write key, value, expires_in = nil
40
+ key = internal_key key
41
+
42
+ do_expiration_check
43
+
44
+ # calculate expiration
45
+ expires_at = Time.now + expires_in.to_i if expires_in.to_i > 0
46
+
47
+ # store data
48
+ if expires_at.nil? || expires_at > Time.now
49
+ entry = Entry.new(value, expires_at)
50
+ @data[key] = entry
51
+ end
52
+
53
+ value
54
+ end
55
+
56
+ def delete key
57
+ key = internal_key key
58
+
59
+ @data.delete key
60
+ end
61
+
62
+ def flush
63
+ @data = {}
64
+ end
65
+
66
+ private
67
+
68
+ def verify_key_not_expired key
69
+ entry = @cache[key]
70
+ verify_entry_not_expired(key, entry) if entry
71
+ end
72
+
73
+ def verify_entry_not_expired key, entry
74
+ if entry.expired?
75
+ @data.delete(key)
76
+ false
77
+ else
78
+ true
79
+ end
80
+ end
81
+
82
+ def do_expiration_check
83
+ @expiration_check_counter += 1
84
+ unless @expiration_check_counter >= @options[:expiration_check_interval]
85
+ return
86
+ end
87
+ @expiration_check_counter = 0
88
+ expire_random_entries
89
+ end
90
+
91
+ def expire_random_entries count = 1_000
92
+ [count, @data.length].min.times do
93
+ key = @data.keys.sample
94
+ break if key.nil?
95
+ entry = @data[key]
96
+ verify_entry_not_expired key, entry
97
+ end
98
+ end
99
+
100
+ def expire_some_entries count = 100
101
+ count = [count, @data.length].min
102
+ @data.each_key do |key|
103
+ break if count <= 0
104
+ count -= 1 unless verify_entry_not_expired(key, entry)
105
+ end
106
+ end
107
+
108
+ def internal_key key
109
+ "#{@options[:cache_key_prefix]}#{key}"
110
+ end
36
111
  end
37
112
  end
38
-
39
- def write key, value, expires_in = nil
40
- key = internal_key key
41
-
42
- do_expiration_check
43
- #value.freeze
44
-
45
- # calculate expiration
46
- expires_at = if expires_in.to_i > 0
47
- Time.now + expires_in.to_i
48
- end
49
-
50
- # store data
51
- if expires_at.nil? || expires_at > Time.now
52
- entry = Entry.new(value, expires_at)
53
- @data[key] = entry
54
- end
55
-
56
- value
57
- end
58
-
59
- def delete key
60
- key = internal_key key
61
-
62
- @data.delete key
63
- end
64
-
65
- private
66
-
67
- def verify_key_not_expired key
68
- entry = @cache[key]
69
- verify_entry_not_expired(key, entry) if entry
70
- end
71
-
72
- def verify_entry_not_expired key, entry
73
- if entry.expired?
74
- @data.delete(key)
75
- false
76
- else
77
- true
78
- end
79
- end
80
-
81
- def do_expiration_check
82
- @expiration_check_counter += 1
83
- unless @expiration_check_counter >= @options[:expiration_check_interval]
84
- return
85
- end
86
- @expiration_check_counter = 0
87
- expire_random_entries
88
- end
89
-
90
- def expire_random_entries count = 1_000
91
- [count, @data.length].min.times do
92
- key, entry = @data.random_pair
93
- break if key.nil?
94
- verify_entry_not_expired key, entry
95
- end
96
- end
97
-
98
- def expire_some_entries count = 100
99
- count = [count, @data.length].min
100
- @data.each_pair do |key, value|
101
- break if count <= 0
102
- count -= 1 unless verify_entry_not_expired(key, entry)
103
- end
104
- end
105
-
106
- def internal_key key
107
- "#{@options[:cache_key_prefix]}#{key}"
108
- end
109
113
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Resterl
4
4
  class Client
5
- attr_reader :options
5
+ attr_reader :options, :cache
6
6
  DEFAULTS = {
7
7
  max_redirect_depth: 10,
8
8
  cache: Resterl::Caches::SimpleCache.new,
@@ -1,3 +1,3 @@
1
1
  module Resterl
2
- VERSION = '0.0.13'
2
+ VERSION = '0.0.14'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resterl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Dütsch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-02 00:00:00.000000000 Z
11
+ date: 2014-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie
@@ -42,14 +42,14 @@ dependencies:
42
42
  name: shoulda
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description:
@@ -94,19 +94,21 @@ require_paths:
94
94
  - lib
95
95
  required_ruby_version: !ruby/object:Gem::Requirement
96
96
  requirements:
97
- - - ! '>='
97
+ - - '>='
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
100
  required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  requirements:
102
- - - ! '>='
102
+ - - '>='
103
103
  - !ruby/object:Gem::Version
104
104
  version: '0'
105
105
  requirements: []
106
106
  rubyforge_project:
107
- rubygems_version: 2.2.2
107
+ rubygems_version: 2.1.11
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Rudimentary HTTP client with focus on caching
111
- test_files: []
111
+ test_files:
112
+ - test/helper.rb
113
+ - test/test_resterl.rb
112
114
  has_rdoc: