readthis 0.8.0 → 0.8.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a07bacb5dfde0174d8c1bbb84f67f4a4fa110a78
4
- data.tar.gz: e33d843547462b265a33333106f68b634e1d22fd
3
+ metadata.gz: ea629e7d22f58baa04359d6fabaee9116caf4e5d
4
+ data.tar.gz: ff5720742c337be8b178ca840064b3469766dcda
5
5
  SHA512:
6
- metadata.gz: 4e50977ab026bfd2df4824b9da0d2ed043005ecd7dd6f747ba431516f6f09d888f51fa5f56619871f462b602e4d4c882e44ab39c80a98d9d0fed48943a97eae6
7
- data.tar.gz: 800a1d90af2fb5a9edd694ae6cea7b319f6e47026394d8dd72fa407c0228766124786f7e22260637baac6a7f36ac4fb8249403c955f6b583334d4f47d823e682
6
+ metadata.gz: b75d0eb316bb68975693ab64044cc3c30159b9511dde446b504e1075b5c6aa915bc3782290334001de3b921173714343c86110778bc41ec90f67dc84b9ef0704
7
+ data.tar.gz: 765fae41b2f8d511cd99269e17ed31eacbb968239e97be78422a4b20793ae3325feefc2f19d411b6d0a17722a7b06bbba78230fe49e12c76e01edabb9fc93b43
data/.travis.yml CHANGED
@@ -1,16 +1,15 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.0.0
5
4
  - 2.1.0
6
5
  - 2.2.2
7
6
  - ruby-head
8
7
  - rbx-2
9
8
  matrix:
10
9
  allow_failures:
11
- - rvm: 2.0.0
12
10
  - rvm: ruby-head
13
11
  - rvm: rbx-2
14
12
  services:
15
13
  - redis-server
16
14
  script: bin/rspec
15
+ bundler_args: --without benchmarking
data/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
1
+ ## v0.8.1 2015-09-04
2
+
3
+ - Changed: `Readthis::Cache` now has an accessor for the options that were
4
+ passed during initialization. This is primarily to support the session store
5
+ middleware provided by `ActionDispatch`. See [readthis#16][issue-16].
6
+ - Fixed: Caching `nil` values is now possible. Previously the value would be
7
+ converted into a blank string, causing a Marshal error when loading the data.
8
+ There is still some non-standard handling of `nil` within `fetch` or
9
+ `fetch_multi`, where a cached `nil` value will always result in a cache miss.
10
+ See [readthis#15][issue-15].
11
+ - Fixed: Entity compression was broken, it wouldn't unload data when the
12
+ compressed size was below the compression limit. Data is now decompressed
13
+ when it can the value looks to be compressed, falling back to the initial
14
+ value when decompression fails. See [readthis#13][issue-13] for details.
15
+
16
+ [issue-13]: https://github.com/sorentwo/readthis/pull/13
17
+ [issue-15]: https://github.com/sorentwo/readthis/pull/15
18
+ [issue-16]: https://github.com/sorentwo/readthis/pull/16
19
+
1
20
  ## v0.8.0 2015-08-26
2
21
 
3
22
  - Breaking: The initializer now takes a single options argument instead of a
data/README.md CHANGED
@@ -34,10 +34,11 @@ Use it the same way as any other [ActiveSupport::Cache::Store][store]. Within a
34
34
  Rails environment config:
35
35
 
36
36
  ```ruby
37
- config.cache_store = :readthis_store,
37
+ config.cache_store = :readthis_store, {
38
38
  expires_in: 2.weeks.to_i,
39
39
  namespace: 'cache',
40
40
  redis: { url: ENV.fetch('REDIS_URL'), driver: :hiredis }
41
+ }
41
42
  ```
42
43
 
43
44
  Otherwise you can use it anywhere, without any reliance on `ActiveSupport`:
@@ -89,9 +90,10 @@ means it is safe to enable or change compression with an existing cache. There
89
90
  will be a decoding performance penalty in this case, but it should be minor.
90
91
 
91
92
  ```ruby
92
- config.cache_store = :readthis_store,
93
+ config.cache_store = :readthis_store, {
93
94
  compress: true,
94
95
  compression_threshold: 2.kilobytes
96
+ }
95
97
  ```
96
98
 
97
99
  ### Marshalling
@@ -118,8 +120,12 @@ Readthis::Cache.new(marshal: Readthis::Passthrough)
118
120
  Readthis supports all of standard cache methods except for the following:
119
121
 
120
122
  * `cleanup` - Redis does this with TTL or LRU already.
121
- * `delete_matched` - You really don't want to perform key matching operations
122
- in Redis. They are linear time and only support basic globbing.
123
+ * `delete_matched` - You really don't want to perform key matching operations in
124
+ Redis. They are linear time and only support basic globbing.
125
+
126
+ Like other `ActiveSupport::Cache` implementations it is possible to cache `nil`
127
+ as a value. However, the fetch methods treat `nil` values as a cache miss and
128
+ re-generate/re-cache the value. Caching `nil` isn't recommended.
123
129
 
124
130
  ## Contributing
125
131
 
@@ -39,14 +39,16 @@ TEXT = <<-TEXT
39
39
  Caches can also store values in a compressed format to save space and reduce time spent sending data. Since there is overhead, values must be large enough to warrant compression. To turn on compression either pass compress: true in the initializer or as an option to fetch or write. To specify the threshold at which to compress values, set the :compress_threshold option. The default threshold is 16K.
40
40
  TEXT
41
41
 
42
- puts 'Compressed Writes:'
42
+ puts 'Compressed Write/Read:'
43
43
  Benchmark.ips do |x|
44
- x.report 'readthis:write' do
44
+ x.report 'readthis:write/read' do
45
45
  readthis.write(KEY, TEXT)
46
+ readthis.read(KEY)
46
47
  end
47
48
 
48
- x.report 'dalli:write' do
49
+ x.report 'dalli:write/read' do
49
50
  dalli.write(KEY, TEXT)
51
+ dalli.read(KEY)
50
52
  end
51
53
 
52
54
  x.compare!
@@ -7,7 +7,7 @@ require 'connection_pool'
7
7
 
8
8
  module Readthis
9
9
  class Cache
10
- attr_reader :entity, :expires_in, :namespace, :pool
10
+ attr_reader :entity, :expires_in, :namespace, :options, :pool
11
11
 
12
12
  # Provide a class level lookup of the proper notifications module.
13
13
  # Instrumention is expected to occur within applications that have
@@ -39,6 +39,7 @@ module Readthis
39
39
  # Readthis::Cache.new(compress: true, compression_threshold: 2048)
40
40
  #
41
41
  def initialize(options = {})
42
+ @options = options
42
43
  @expires_in = options.fetch(:expires_in, nil)
43
44
  @namespace = options.fetch(:namespace, nil)
44
45
 
@@ -3,6 +3,7 @@ require 'zlib'
3
3
  module Readthis
4
4
  class Entity
5
5
  DEFAULT_THRESHOLD = 8 * 1024
6
+ MAGIC_BYTES = [120, 156].freeze
6
7
 
7
8
  attr_reader :marshal, :compression, :threshold
8
9
 
@@ -13,8 +14,6 @@ module Readthis
13
14
  end
14
15
 
15
16
  def dump(value)
16
- return value if value.nil?
17
-
18
17
  if compress?(value)
19
18
  compress(value)
20
19
  else
@@ -23,9 +22,7 @@ module Readthis
23
22
  end
24
23
 
25
24
  def load(value)
26
- return value if value.nil?
27
-
28
- if compress?(value)
25
+ if compressed?(value)
29
26
  decompress(value)
30
27
  else
31
28
  marshal.load(value)
@@ -47,5 +44,9 @@ module Readthis
47
44
  def compress?(value)
48
45
  compression && value.bytesize >= threshold
49
46
  end
47
+
48
+ def compressed?(value)
49
+ compression && value[0, 2].unpack('CC') == MAGIC_BYTES
50
+ end
50
51
  end
51
52
  end
@@ -1,3 +1,3 @@
1
1
  module Readthis
2
- VERSION = '0.8.0'
2
+ VERSION = '0.8.1'
3
3
  end
@@ -19,6 +19,12 @@ RSpec.describe Readthis::Cache do
19
19
 
20
20
  expect(cache.expires_in).to eq(10)
21
21
  end
22
+
23
+ it 'makes options available' do
24
+ cache = Readthis::Cache.new(namespace: 'cache', expires_in: 1)
25
+
26
+ expect(cache.options).to eq(namespace: 'cache', expires_in: 1)
27
+ end
22
28
  end
23
29
 
24
30
  describe '#pool' do
@@ -25,10 +25,24 @@ RSpec.describe Readthis::Entity do
25
25
  expect(entity.dump(string)).not_to eq(dumped)
26
26
  end
27
27
 
28
- it 'does not dump nil values' do
28
+ it 'does not return compressed data when the size is below threshold' do
29
+ string = 'a' * 200
30
+ entity = Readthis::Entity.new(compress: true, threshold: 50)
31
+
32
+ expect(entity.load(entity.dump(string))).to eq(string)
33
+ end
34
+
35
+ it 'safely returns incorrectly deduced compressed data' do
36
+ string = [120, 156, 97, 98, 99].pack('CCCCC')
37
+ entity = Readthis::Entity.new(compress: true, threshold: 1)
38
+
39
+ expect(entity.load(string)).to eq(string)
40
+ end
41
+
42
+ it 'safely roundtrips nil values' do
29
43
  entity = Readthis::Entity.new
30
44
 
31
- expect(entity.dump(nil)).to be_nil
45
+ expect(entity.load(entity.dump(nil))).to be_nil
32
46
  end
33
47
  end
34
48
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: readthis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Parker Selbert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-26 00:00:00.000000000 Z
11
+ date: 2015-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis