cache_driver 0.2.1 → 0.3.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.
- checksums.yaml +4 -4
- data/cache_driver.gemspec +2 -0
- data/lib/cache_driver.rb +1 -0
- data/lib/cache_driver/cache_record.rb +24 -31
- data/lib/cache_driver/cache_util.rb +28 -28
- data/lib/cache_driver/config.rb +16 -17
- data/lib/cache_driver/file_cache_util.rb +73 -104
- data/lib/cache_driver/redis_cache_util.rb +63 -0
- data/lib/cache_driver/version.rb +1 -1
- metadata +18 -5
- data/pkg/cache_driver-0.1.0.gem +0 -0
- data/pkg/cache_driver-0.2.0.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 402b06942adc9a03461be8eed481772cf10c1b09
|
4
|
+
data.tar.gz: d9d317fc326141587e83dd42483387c42475e27c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd99e95e14c5261382c8e3e3ee89d6e7db524205ba4c9ee6022e580ad8a6613c2e509ce2a6d7af603cb84178a694c26a10af64e75588e7da21e0c73e16d1ad53
|
7
|
+
data.tar.gz: 47059c2b1880671f93c627f4270acce731339b29ca9296b72e66e9119dbe6d75ecf70a47c7b4ba7386c169d0dabc8f48d65782117b15edc52ca5a1df8a298874
|
data/cache_driver.gemspec
CHANGED
data/lib/cache_driver.rb
CHANGED
@@ -1,38 +1,31 @@
|
|
1
1
|
class CacheRecord
|
2
|
-
|
3
|
-
if CacheDriver.store_file?
|
4
|
-
FileCacheUtil.read_all CacheUtil.class_to_type(self)
|
5
|
-
end
|
6
|
-
end
|
2
|
+
@@cache_util = CacheDriver.store_file? ? FileCacheUtil : CacheDriver.store_redis? ? RedisCacheUtil : nil
|
7
3
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
end
|
4
|
+
def self.find_all
|
5
|
+
@@cache_util.read_all CacheUtil.class_to_type(self)
|
6
|
+
end
|
13
7
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
8
|
+
def self.find_by_key(key)
|
9
|
+
@@cache_util.read CacheUtil.class_to_type(self), id
|
10
|
+
end
|
19
11
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
24
|
-
end
|
12
|
+
def save!
|
13
|
+
@@cache_util.write CacheUtil.class_to_type(self.class), self
|
14
|
+
end
|
25
15
|
|
26
|
-
|
27
|
-
|
28
|
-
|
16
|
+
def destroy
|
17
|
+
@@cache_util.delete CacheUtil.class_to_type(self.class), self.id
|
18
|
+
end
|
29
19
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
20
|
+
def to_cache
|
21
|
+
self.to_json
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.from_cache(obj)
|
25
|
+
ins = self.new
|
26
|
+
obj.each do |key, value|
|
27
|
+
ins.send "#{key}=", value
|
28
|
+
end
|
29
|
+
ins
|
30
|
+
end
|
38
31
|
end
|
@@ -1,36 +1,36 @@
|
|
1
1
|
class CacheUtil
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
class << self
|
4
|
+
def write(type, key, data)
|
5
|
+
puts "[CACHE] save #{type} ##{key} to cache: #{data}"
|
6
|
+
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
def read(type, key)
|
9
|
+
puts "[CACHE] get #{type} ##{key} from cache"
|
10
|
+
end
|
11
|
+
|
12
|
+
def read_all(type)
|
13
|
+
puts "[CACHE] get all #{type} from cache"
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
def delete(type, key)
|
17
|
+
puts "[CACHE] delete #{type} ##{key} from cache"
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
# type --> :room
|
21
|
+
# class --> Room
|
22
|
+
# dir --> 'rooms'
|
23
|
+
def type_to_class(type)
|
24
|
+
type.to_s.capitalize.constantize
|
25
|
+
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
27
|
+
def class_to_type(clazz)
|
28
|
+
cla_str = clazz.name
|
29
|
+
(cla_str[0].downcase + cla_str[1..-1]).to_sym
|
30
|
+
end
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
def type_to_dir(type)
|
33
|
+
type.to_s + 's'
|
34
|
+
end
|
35
|
+
end
|
36
36
|
end
|
data/lib/cache_driver/config.rb
CHANGED
@@ -1,24 +1,23 @@
|
|
1
1
|
module CacheDriver
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
class Config
|
3
|
+
attr_accessor :store, :file_dir, :redis_host, :redis_port, :redis_namespace
|
4
|
+
end
|
5
5
|
|
6
|
-
|
6
|
+
def self.configed?
|
7
|
+
@@config.store
|
8
|
+
end
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
def self.store_file?
|
11
|
+
@@config.store == :file
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
14
|
+
def self.store_redis?
|
15
|
+
@@config.store == :redis
|
16
|
+
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
def self.setup
|
21
|
-
yield @@config
|
22
|
-
end
|
18
|
+
def self.setup
|
19
|
+
@@config = Config.new
|
20
|
+
yield @@config
|
21
|
+
end
|
23
22
|
|
24
23
|
end
|
@@ -1,106 +1,75 @@
|
|
1
1
|
class FileCacheUtil < CacheUtil
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
dir_scaner = Dir.new dir
|
76
|
-
|
77
|
-
data = []
|
78
|
-
dir_scaner.each do |file_name|
|
79
|
-
file = File.new dir.join(file_name), 'r'
|
80
|
-
next unless File.file?(file) and File.extname(file) == ".cache"
|
81
|
-
data_str = file.read.split("\t")[1]
|
82
|
-
|
83
|
-
unless data_str
|
84
|
-
puts "cache #{type}-#{file} data miss"
|
85
|
-
next
|
86
|
-
end
|
87
|
-
|
88
|
-
data << type_to_class(type).from_cache(data_str)
|
89
|
-
end
|
90
|
-
|
91
|
-
data
|
92
|
-
end
|
93
|
-
|
94
|
-
def delete(type, data_id)
|
95
|
-
super type, data_id
|
96
|
-
|
97
|
-
dir = CACHE_DIR.join type_to_dir(type)
|
98
|
-
return true unless Dir.exist? dir
|
99
|
-
|
100
|
-
file_path = dir.join("#{data_id}.cache")
|
101
|
-
return true unless File.exist? file_path
|
102
|
-
File.delete file_path
|
103
|
-
true
|
104
|
-
end
|
105
|
-
end
|
2
|
+
@@file_dir = CacheDriver.file_dir
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def write(type, key, data)
|
6
|
+
super type, data
|
7
|
+
|
8
|
+
dir = @@file_dir.join type_to_dir(type)
|
9
|
+
Dir.mkdir dir unless Dir.exist? dir
|
10
|
+
|
11
|
+
file = File.new dir.join("#{key}.cache"), 'w'
|
12
|
+
file.puts "#{Time.now} --> #{data.to_cache}"
|
13
|
+
file.close
|
14
|
+
|
15
|
+
key
|
16
|
+
end
|
17
|
+
|
18
|
+
def read(type, key)
|
19
|
+
super type, key
|
20
|
+
|
21
|
+
dir = @@file_dir.join type_to_dir(type)
|
22
|
+
Dir.mkdir dir unless Dir.exist? dir
|
23
|
+
|
24
|
+
file_path = dir.join("#{key}.cache")
|
25
|
+
return nil unless File.exist? file_path
|
26
|
+
file = File.new file_path, 'r'
|
27
|
+
data_str = file.read.split(" --> ")[1]
|
28
|
+
|
29
|
+
unless data_str
|
30
|
+
puts "cache #{type} ##{key} data miss"
|
31
|
+
return nil
|
32
|
+
end
|
33
|
+
|
34
|
+
data = JSON.parse data_str
|
35
|
+
type_to_class(type).from_cache data
|
36
|
+
end
|
37
|
+
|
38
|
+
def read_all(type)
|
39
|
+
super type
|
40
|
+
|
41
|
+
dir = @@file_dir.join type_to_dir(type)
|
42
|
+
Dir.mkdir dir unless Dir.exist? dir
|
43
|
+
dir_scaner = Dir.new dir
|
44
|
+
|
45
|
+
data = []
|
46
|
+
dir_scaner.each do |file_name|
|
47
|
+
file = File.new dir.join(file_name), 'r'
|
48
|
+
next unless File.file?(file) and File.extname(file) == ".cache"
|
49
|
+
data_str = file.read.split(" --> ")[1]
|
50
|
+
|
51
|
+
unless data_str
|
52
|
+
puts "cache #{type} ##{file_name} data miss"
|
53
|
+
next
|
54
|
+
end
|
55
|
+
|
56
|
+
d = JSON.parse data_str
|
57
|
+
data << type_to_class(type).from_cache(d)
|
58
|
+
end
|
59
|
+
|
60
|
+
data
|
61
|
+
end
|
62
|
+
|
63
|
+
def delete(type, key)
|
64
|
+
super type, key
|
65
|
+
|
66
|
+
dir = @@file_dir.join type_to_dir(type)
|
67
|
+
return true unless Dir.exist? dir
|
68
|
+
|
69
|
+
file_path = dir.join("#{key}.cache")
|
70
|
+
return true unless File.exist? file_path
|
71
|
+
File.delete file_path
|
72
|
+
true
|
73
|
+
end
|
74
|
+
end
|
106
75
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
class RedisCacheUtil < CacheUtil
|
2
|
+
@@redis = Redis.new :host => CacheDriver.redis_host, :port => CacheDriver.redis_port
|
3
|
+
@@namespace = CacheDriver.redis_namespace
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def write(type, key, data)
|
7
|
+
super type, data
|
8
|
+
|
9
|
+
res = @@redis.set "#{@@namespace}:#{type_to_dir(type)}##{key}", "#{Time.now} --> #{data.to_cache}"
|
10
|
+
res == "OK" ? key : nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def read(type, key)
|
14
|
+
super type, key
|
15
|
+
|
16
|
+
content = @@redis.get("#{@@namespace}:#{type_to_dir(type)}##{key}")
|
17
|
+
unless content
|
18
|
+
puts "cache #{type} ##{key} data miss"
|
19
|
+
return nil
|
20
|
+
end
|
21
|
+
|
22
|
+
data_str = file.read.split(" --> ")[1]
|
23
|
+
unless data_str
|
24
|
+
puts "cache #{type} ##{key} data miss"
|
25
|
+
return nil
|
26
|
+
end
|
27
|
+
|
28
|
+
data = JSON.parse data_str
|
29
|
+
type_to_class(type).from_cache data
|
30
|
+
end
|
31
|
+
|
32
|
+
def read_all(type)
|
33
|
+
super type
|
34
|
+
|
35
|
+
data = []
|
36
|
+
@@redis.keys("#{@@namespace}:#{type_to_dir(type)}#*").each do |key|
|
37
|
+
content = @@redis.get(key)
|
38
|
+
unless content
|
39
|
+
puts "cache #{key} data miss"
|
40
|
+
next
|
41
|
+
end
|
42
|
+
|
43
|
+
data_str = file.read.split(" --> ")[1]
|
44
|
+
unless data_str
|
45
|
+
puts "cache #{key} data miss"
|
46
|
+
next
|
47
|
+
end
|
48
|
+
|
49
|
+
d = JSON.parse data_str
|
50
|
+
data << type_to_class(type).from_cache(d)
|
51
|
+
end
|
52
|
+
|
53
|
+
data
|
54
|
+
end
|
55
|
+
|
56
|
+
def delete(type, key)
|
57
|
+
super type, key
|
58
|
+
|
59
|
+
res = @@redis.del("#{@@namespace}:#{type_to_dir(type)}##{key}")
|
60
|
+
res == 1
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/cache_driver/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_driver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- goshan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-11-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: redis
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.3'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.3'
|
41
55
|
description: make rails model act as ActiveRecord, but not save data into database
|
42
56
|
but cache system, file or redis
|
43
57
|
email:
|
@@ -57,10 +71,9 @@ files:
|
|
57
71
|
- lib/cache_driver/cache_util.rb
|
58
72
|
- lib/cache_driver/config.rb
|
59
73
|
- lib/cache_driver/file_cache_util.rb
|
74
|
+
- lib/cache_driver/redis_cache_util.rb
|
60
75
|
- lib/cache_driver/version.rb
|
61
76
|
- lib/tasks/cache.rake
|
62
|
-
- pkg/cache_driver-0.1.0.gem
|
63
|
-
- pkg/cache_driver-0.2.0.gem
|
64
77
|
- tmp/cache/empty
|
65
78
|
homepage: https://github.com/goshan/cache_driver
|
66
79
|
licenses:
|
@@ -82,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
95
|
version: '0'
|
83
96
|
requirements: []
|
84
97
|
rubyforge_project:
|
85
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.5.2
|
86
99
|
signing_key:
|
87
100
|
specification_version: 4
|
88
101
|
summary: A cache adapter for model accepting file or redis
|
data/pkg/cache_driver-0.1.0.gem
DELETED
Binary file
|
data/pkg/cache_driver-0.2.0.gem
DELETED
Binary file
|