large_object_store 0.0.1 → 1.0.0

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/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- large_object_store (0.0.1)
4
+ large_object_store (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Readme.md CHANGED
@@ -1,6 +1,7 @@
1
1
  Store large objects in memcache or others by slicing them.
2
2
  - uses read_multi for fast access
3
3
  - returns nil if one slice is missing
4
+ - only uses single read/write if data is below 1MB
4
5
 
5
6
  Install
6
7
  =======
@@ -1,3 +1,3 @@
1
1
  module LargeObjectStore
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -20,18 +20,22 @@ module LargeObjectStore
20
20
 
21
21
  # store number of pages
22
22
  pages = (value.size / LIMIT.to_f).ceil
23
- @store.write("#{key}_0", pages, options)
24
23
 
25
- # store object
26
- page = 1
27
- loop do
28
- slice = value.slice!(0, LIMIT)
29
- break if slice.size == 0
24
+ if pages == 1
25
+ @store.write("#{key}_0", value, options)
26
+ else
27
+ @store.write("#{key}_0", pages, options)
30
28
 
31
- @store.write("#{key}_#{page}", slice, options)
32
- page += 1
33
- end
29
+ # store object
30
+ page = 1
31
+ loop do
32
+ slice = value.slice!(0, LIMIT)
33
+ break if slice.size == 0
34
34
 
35
+ @store.write("#{key}_#{page}", slice, options)
36
+ page += 1
37
+ end
38
+ end
35
39
  true
36
40
  end
37
41
 
@@ -40,11 +44,16 @@ module LargeObjectStore
40
44
  pages = @store.read("#{key}_0")
41
45
  return if pages.nil?
42
46
 
43
- # read sliced data
44
- keys = Array.new(pages).each_with_index.map{|_,i| "#{key}_#{i+1}" }
45
- slices = @store.read_multi(*keys).values
46
- return nil if slices.compact.size < pages
47
- Marshal.load(slices.join(""))
47
+ data = if pages.is_a?(String)
48
+ pages
49
+ else
50
+ # read sliced data
51
+ keys = Array.new(pages).each_with_index.map{|_,i| "#{key}_#{i+1}" }
52
+ slices = @store.read_multi(*keys).values
53
+ return nil if slices.compact.size < pages
54
+ slices.join("")
55
+ end
56
+ Marshal.load(data)
48
57
  end
49
58
 
50
59
  def fetch(key, options={})
@@ -47,11 +47,16 @@ describe LargeObjectStore do
47
47
  store.read("a").size.should == 10_000_000
48
48
  end
49
49
 
50
- it "passes options" do
51
- store.store.should_receive(:write).with(anything, anything, :expires_in => 111).twice
50
+ it "passes options when caching small" do
51
+ store.store.should_receive(:write).with(anything, anything, :expires_in => 111)
52
52
  store.write("a", "a", :expires_in => 111)
53
53
  end
54
54
 
55
+ it "passes options when caching big" do
56
+ store.store.should_receive(:write).with(anything, anything, :expires_in => 111).exactly(3).times
57
+ store.write("a", "a"*1_200_000, :expires_in => 111)
58
+ end
59
+
55
60
  it "cannot read corrupted objects" do
56
61
  store.write("a", ["a"*10_000_000]).should == true
57
62
  store.store.write("a_4", nil)
@@ -78,6 +83,11 @@ describe LargeObjectStore do
78
83
  store.store.keys.should == ["a_0", "a_1", "a_2", "a_3", "a_4", "a_5"]
79
84
  end
80
85
 
86
+ it "uses 1 key when value is small enough" do
87
+ store.write("a", "a"*500_000)
88
+ store.store.keys.should == ["a_0"]
89
+ end
90
+
81
91
  it "uses read_multi" do
82
92
  store.write("a", "a"*5_000_000)
83
93
  store.store.should_receive(:read).with("a_0").and_return 5
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: large_object_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.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-17 00:00:00.000000000 Z
12
+ date: 2013-05-21 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: acemacu@gmail.com