uber_cache 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/uber_cache.rb +94 -0
  2. metadata +78 -0
data/lib/uber_cache.rb ADDED
@@ -0,0 +1,94 @@
1
+ require 'dalli'
2
+
3
+ class UberCache
4
+ OBJ_MAX_SIZE = 1000000
5
+
6
+ @client = nil
7
+ @cache_prefix
8
+ @hosts
9
+
10
+ def initialize(cache_prefix, hosts)
11
+ @cache_prefix = cache_prefix
12
+ @hosts = hosts
13
+ end
14
+
15
+ # read from the cache
16
+ def read(key)
17
+ client.get(keyify(key))
18
+ end
19
+
20
+ # write to the cache - value will pull from passed block if block is passed.
21
+ def write(key, value = nil, opts = {}, &blk)
22
+ value = blk.call() if blk
23
+ ttl = opts[:ttl]
24
+ client.set(keyify(key), value, ttl)
25
+ end
26
+
27
+ def read_or_write(key, opts = {}, &blk)
28
+ found = client.get(keyify(key))
29
+ return found unless found.nil?
30
+ value = nil
31
+ value = blk.call() if blk
32
+ ttl = opts[:ttl]
33
+ client.set(keyify(key), value, ttl)
34
+ return value
35
+ end
36
+
37
+ def clear(key)
38
+ client.set(keyify(key), nil)
39
+ end
40
+
41
+ def clear_all
42
+ client.flush
43
+ end
44
+
45
+ #UberObjectCache
46
+ def obj_read(master_key)
47
+ data = []
48
+ segment = 0
49
+ while(more_data = read("#{master_key}-#{segment}"))
50
+ data << more_data
51
+ segment += 1
52
+ end
53
+ return nil if data.length == 0
54
+ return Marshal::load(data.join(""))
55
+ end
56
+
57
+ def obj_write(master_key, obj = nil, opts = {}, &blk)
58
+ obj = blk.call() if blk
59
+ max_size = opts.delete(:max_size) || OBJ_MAX_SIZE
60
+ data = Marshal::dump(obj)
61
+ segment = 0
62
+ while(data)
63
+ write("#{master_key}-#{segment}", data.slice(0, max_size), opts)
64
+ data = data.slice(max_size, data.length)
65
+ segment += 1
66
+ end
67
+ clear("#{master_key}-#{segment}")
68
+ end
69
+
70
+ def obj_clear(master_key)
71
+ clear("#{master_key}-0")
72
+ end
73
+
74
+ def obj_read_or_write(master_key, opts = {}, &blk)
75
+ found = obj_read(master_key)
76
+ reload = opts.delete(:reload) || false
77
+ return found if found && !reload
78
+ value = nil
79
+ value = blk.call() if blk
80
+ obj_write(master_key, value, opts)
81
+ return value
82
+ end
83
+
84
+ private
85
+ def client
86
+ @client ||= Dalli::Client.new(@hosts)
87
+ end
88
+
89
+ #namespace the keys - to not kill other stuff.
90
+ def keyify(key)
91
+ "#{@cache_prefix}:#{key}"
92
+ end
93
+
94
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uber_cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Reister
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: dalli
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Simple Caching Wrapper for Dalli/Memcache
47
+ email: chris@chrisreister.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/uber_cache.rb
53
+ homepage: https://github.com/chrisftw/uber_cache
54
+ licenses:
55
+ - MIT
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.23
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Simple Caching Wrapper for Dalli/Memcache
78
+ test_files: []