cache_driver 0.3.0 → 0.3.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 +4 -4
- data/README.md +14 -11
- data/lib/cache_driver/cache_record.rb +9 -5
- data/lib/cache_driver/cache_util.rb +1 -1
- data/lib/cache_driver/config.rb +6 -1
- data/lib/cache_driver/file_cache_util.rb +11 -7
- data/lib/cache_driver/redis_cache_util.rb +18 -10
- data/lib/cache_driver/version.rb +1 -1
- data/lib/cache_driver.rb +2 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d83400081bd047d18a81a8963c3ff153907b8e6
|
4
|
+
data.tar.gz: 2ad7f261510c4a34f30a11d28e70029fb9f1927b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7031b3a78d2e6295d3007b0d6083cebd12b9224c57c21e7e7d11b7a874ce422bee03be770df6b53509e214317ebdd8feceb6ef89f9f4a16c1d5a318c92661ff0
|
7
|
+
data.tar.gz: 3921d0cf509f78461e581b0e6e18114a1d13dc9b1ab63f7b24afdb02175953859cdac74b3ef4731bbafce01a501febc2618dbc76773bffe18a1a7723c717dd50
|
data/README.md
CHANGED
@@ -20,18 +20,17 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
$ touch config/initializers/cache_driver.rb
|
23
|
+
Add following code to environments/development.rb or environments/production.rb
|
26
24
|
|
27
25
|
```ruby
|
28
|
-
# CacheDriver Config
|
29
|
-
# to make model save data to file or redis not database
|
30
|
-
|
31
26
|
CacheDriver.setup do |config|
|
32
27
|
# set cache store type, :file or :redis
|
33
|
-
# default is :file
|
34
28
|
config.store = :file
|
29
|
+
config.file_dir = Rails.root.join('tmp', 'cache')
|
30
|
+
#config.store = :redis
|
31
|
+
#config.redis_host = "127.0.0.1"
|
32
|
+
#config.redis_port = 6379
|
33
|
+
#config.redis_namespace = "namespace"
|
35
34
|
end
|
36
35
|
```
|
37
36
|
|
@@ -43,6 +42,10 @@ Just define model like below
|
|
43
42
|
|
44
43
|
```ruby
|
45
44
|
class CacheModel < CacheRecord
|
45
|
+
# attribute name which is made as key to cache data
|
46
|
+
def self.key_attr
|
47
|
+
"attr_name"
|
48
|
+
end
|
46
49
|
end
|
47
50
|
```
|
48
51
|
|
@@ -51,7 +54,7 @@ So you can use this model just like other ActiveRecord instance in controllers l
|
|
51
54
|
```ruby
|
52
55
|
cache_models = CacheModel.find_all # get all instance in cache
|
53
56
|
|
54
|
-
cache_model = CacheModel.
|
57
|
+
cache_model = CacheModel.find_by_key key # get instance with key
|
55
58
|
|
56
59
|
cache_model.save! # save this instance to cache
|
57
60
|
|
@@ -63,11 +66,11 @@ Just override the two methods below
|
|
63
66
|
|
64
67
|
```ruby
|
65
68
|
def to_cache
|
66
|
-
#
|
69
|
+
# return a hashmap which you want to cache for this model
|
67
70
|
end
|
68
71
|
|
69
|
-
def self.from_cache
|
70
|
-
#
|
72
|
+
def self.from_cache(obj)
|
73
|
+
# return this model instance from a hashmap obj include data you cached
|
71
74
|
end
|
72
75
|
```
|
73
76
|
|
@@ -1,20 +1,19 @@
|
|
1
1
|
class CacheRecord
|
2
|
-
@@cache_util = CacheDriver.store_file? ? FileCacheUtil : CacheDriver.store_redis? ? RedisCacheUtil : nil
|
3
2
|
|
4
3
|
def self.find_all
|
5
|
-
|
4
|
+
CacheRecord.cache_util.read_all CacheUtil.class_to_type(self)
|
6
5
|
end
|
7
6
|
|
8
7
|
def self.find_by_key(key)
|
9
|
-
|
8
|
+
CacheRecord.cache_util.read CacheUtil.class_to_type(self), key
|
10
9
|
end
|
11
10
|
|
12
11
|
def save!
|
13
|
-
|
12
|
+
CacheRecord.cache_util.write CacheUtil.class_to_type(self.class), self.send(self.class.key_attr), self
|
14
13
|
end
|
15
14
|
|
16
15
|
def destroy
|
17
|
-
|
16
|
+
CacheRecord.cache_util.delete CacheUtil.class_to_type(self.class), self.send(self.class.key_attr)
|
18
17
|
end
|
19
18
|
|
20
19
|
def to_cache
|
@@ -28,4 +27,9 @@ class CacheRecord
|
|
28
27
|
end
|
29
28
|
ins
|
30
29
|
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def self.cache_util
|
33
|
+
CacheDriver.store_file? ? FileCacheUtil : CacheDriver.store_redis? ? RedisCacheUtil : nil
|
34
|
+
end
|
31
35
|
end
|
data/lib/cache_driver/config.rb
CHANGED
@@ -3,6 +3,12 @@ module CacheDriver
|
|
3
3
|
attr_accessor :store, :file_dir, :redis_host, :redis_port, :redis_namespace
|
4
4
|
end
|
5
5
|
|
6
|
+
@@config = Config.new
|
7
|
+
|
8
|
+
def self.config
|
9
|
+
@@config
|
10
|
+
end
|
11
|
+
|
6
12
|
def self.configed?
|
7
13
|
@@config.store
|
8
14
|
end
|
@@ -16,7 +22,6 @@ module CacheDriver
|
|
16
22
|
end
|
17
23
|
|
18
24
|
def self.setup
|
19
|
-
@@config = Config.new
|
20
25
|
yield @@config
|
21
26
|
end
|
22
27
|
|
@@ -1,15 +1,14 @@
|
|
1
1
|
class FileCacheUtil < CacheUtil
|
2
|
-
@@file_dir = CacheDriver.file_dir
|
3
2
|
|
4
3
|
class << self
|
5
4
|
def write(type, key, data)
|
6
|
-
super type, data
|
5
|
+
super type, key, data
|
7
6
|
|
8
|
-
dir =
|
7
|
+
dir = file_dir.join type_to_dir(type)
|
9
8
|
Dir.mkdir dir unless Dir.exist? dir
|
10
9
|
|
11
10
|
file = File.new dir.join("#{key}.cache"), 'w'
|
12
|
-
file.puts "#{Time.now} --> #{data.to_cache}"
|
11
|
+
file.puts "#{Time.now} --> #{data.to_cache.to_json}"
|
13
12
|
file.close
|
14
13
|
|
15
14
|
key
|
@@ -18,7 +17,7 @@ class FileCacheUtil < CacheUtil
|
|
18
17
|
def read(type, key)
|
19
18
|
super type, key
|
20
19
|
|
21
|
-
dir =
|
20
|
+
dir = file_dir.join type_to_dir(type)
|
22
21
|
Dir.mkdir dir unless Dir.exist? dir
|
23
22
|
|
24
23
|
file_path = dir.join("#{key}.cache")
|
@@ -38,7 +37,7 @@ class FileCacheUtil < CacheUtil
|
|
38
37
|
def read_all(type)
|
39
38
|
super type
|
40
39
|
|
41
|
-
dir =
|
40
|
+
dir = file_dir.join type_to_dir(type)
|
42
41
|
Dir.mkdir dir unless Dir.exist? dir
|
43
42
|
dir_scaner = Dir.new dir
|
44
43
|
|
@@ -63,7 +62,7 @@ class FileCacheUtil < CacheUtil
|
|
63
62
|
def delete(type, key)
|
64
63
|
super type, key
|
65
64
|
|
66
|
-
dir =
|
65
|
+
dir = file_dir.join type_to_dir(type)
|
67
66
|
return true unless Dir.exist? dir
|
68
67
|
|
69
68
|
file_path = dir.join("#{key}.cache")
|
@@ -71,5 +70,10 @@ class FileCacheUtil < CacheUtil
|
|
71
70
|
File.delete file_path
|
72
71
|
true
|
73
72
|
end
|
73
|
+
|
74
|
+
private
|
75
|
+
def file_dir
|
76
|
+
CacheDriver.config.file_dir
|
77
|
+
end
|
74
78
|
end
|
75
79
|
end
|
@@ -1,25 +1,23 @@
|
|
1
1
|
class RedisCacheUtil < CacheUtil
|
2
|
-
@@redis = Redis.new :host => CacheDriver.redis_host, :port => CacheDriver.redis_port
|
3
|
-
@@namespace = CacheDriver.redis_namespace
|
4
2
|
|
5
3
|
class << self
|
6
4
|
def write(type, key, data)
|
7
|
-
super type, data
|
5
|
+
super type, key, data
|
8
6
|
|
9
|
-
res =
|
7
|
+
res = redis.set "#{namespace}:#{type_to_dir(type)}##{key}", "#{Time.now} --> #{data.to_cache.to_json}"
|
10
8
|
res == "OK" ? key : nil
|
11
9
|
end
|
12
10
|
|
13
11
|
def read(type, key)
|
14
12
|
super type, key
|
15
13
|
|
16
|
-
content =
|
14
|
+
content = redis.get("#{namespace}:#{type_to_dir(type)}##{key}")
|
17
15
|
unless content
|
18
16
|
puts "cache #{type} ##{key} data miss"
|
19
17
|
return nil
|
20
18
|
end
|
21
19
|
|
22
|
-
data_str =
|
20
|
+
data_str = content.split(" --> ")[1]
|
23
21
|
unless data_str
|
24
22
|
puts "cache #{type} ##{key} data miss"
|
25
23
|
return nil
|
@@ -32,15 +30,16 @@ class RedisCacheUtil < CacheUtil
|
|
32
30
|
def read_all(type)
|
33
31
|
super type
|
34
32
|
|
33
|
+
r = self.redis
|
35
34
|
data = []
|
36
|
-
|
37
|
-
content =
|
35
|
+
r.keys("#{namespace}:#{type_to_dir(type)}#*").each do |key|
|
36
|
+
content = r.get(key)
|
38
37
|
unless content
|
39
38
|
puts "cache #{key} data miss"
|
40
39
|
next
|
41
40
|
end
|
42
41
|
|
43
|
-
data_str =
|
42
|
+
data_str = content.split(" --> ")[1]
|
44
43
|
unless data_str
|
45
44
|
puts "cache #{key} data miss"
|
46
45
|
next
|
@@ -56,8 +55,17 @@ class RedisCacheUtil < CacheUtil
|
|
56
55
|
def delete(type, key)
|
57
56
|
super type, key
|
58
57
|
|
59
|
-
res =
|
58
|
+
res = redis.del("#{namespace}:#{type_to_dir(type)}##{key}")
|
60
59
|
res == 1
|
61
60
|
end
|
61
|
+
|
62
|
+
private
|
63
|
+
def redis
|
64
|
+
Redis.new :host => CacheDriver.config.redis_host, :port => CacheDriver.config.redis_port
|
65
|
+
end
|
66
|
+
|
67
|
+
def namespace
|
68
|
+
CacheDriver.config.redis_namespace
|
69
|
+
end
|
62
70
|
end
|
63
71
|
end
|
data/lib/cache_driver/version.rb
CHANGED
data/lib/cache_driver.rb
CHANGED