cache_driver 0.1.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 +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/cache_driver.gemspec +30 -0
- data/lib/cache_driver.rb +11 -0
- data/lib/cache_driver/cache_record.rb +33 -0
- data/lib/cache_driver/cache_util.rb +36 -0
- data/lib/cache_driver/config.rb +24 -0
- data/lib/cache_driver/file_cache_util.rb +106 -0
- data/lib/cache_driver/version.rb +3 -0
- data/tmp/cache/empty +0 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 674922fba3ded31a1640ac8974c798f591f7132d
|
4
|
+
data.tar.gz: dff31e48f7763fa0e7d68693bf787997c64b8ec0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 04f2f65372264fe96ce6318ad63c30ce06e2d9f5fc4404e220385064ac81fad5fde57c9837c2d4be0a9b99ed547301ed774654c1853b978299dd57c9fb9e634e
|
7
|
+
data.tar.gz: 7635ed4b3eab7ddd00bbacf0b54ac2a04ca10d70e823cd148dd4440f92892d7f538e119c9e393cabfbe0998a79e1dcb99121ef06690b076ea4c345ccc4db92f9
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 goshan Chio
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# CacheDriver
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cache_driver`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'cache_driver'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install cache_driver
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cache_driver. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cache_driver/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cache_driver"
|
8
|
+
spec.version = CacheDriver::VERSION
|
9
|
+
spec.authors = ["goshan"]
|
10
|
+
spec.email = ["goshan.hanqiu@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A cache adapter for model accepting file or redis}
|
13
|
+
spec.description = %q{make rails model act as ActiveRecord, but not save data into database but cache system, file or redis}
|
14
|
+
spec.homepage = "https://github.com/goshan/cache_driver"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
30
|
+
end
|
data/lib/cache_driver.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "cache_driver/version"
|
2
|
+
require "cache_driver/config"
|
3
|
+
require "cache_driver/cache_record"
|
4
|
+
require "cache_driver/cache_util"
|
5
|
+
require "cache_driver/file_cache_util"
|
6
|
+
|
7
|
+
unless CacheDriver.configed?
|
8
|
+
CacheDriver.setup do |config|
|
9
|
+
config.store = :file
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class CacheRecord
|
2
|
+
def self.find_all
|
3
|
+
if CacheDriver.store_file?
|
4
|
+
FileCacheUtil.read_all CacheUtil.class_to_type(self)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.find_by_id(id)
|
9
|
+
if CacheDriver.store_file?
|
10
|
+
FileCacheUtil.read CacheUtil.class_to_type(self), id
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def save!
|
15
|
+
if CacheDriver.store_file?
|
16
|
+
FileCacheUtil.write CacheUtil.class_to_type(self.class), self
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def destroy
|
21
|
+
if CacheDriver.store_file?
|
22
|
+
FileCacheUtil.delete CacheUtil.class_to_type(self.class), self.id
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_cache
|
27
|
+
self.inspect
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.from_cache(str)
|
31
|
+
self.new
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class CacheUtil
|
2
|
+
|
3
|
+
class << self
|
4
|
+
def write(type, data)
|
5
|
+
puts "[CACHE] save #{type} to cache: #{data}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def read(type, data_id)
|
9
|
+
puts "[CACHE] get #{type} from cache: #{data_id}"
|
10
|
+
end
|
11
|
+
|
12
|
+
def read_all(type)
|
13
|
+
puts "[CACHE] get #{type} from cache: all"
|
14
|
+
end
|
15
|
+
|
16
|
+
def delete(type, data_id)
|
17
|
+
puts "[CACHE] delete #{type} from cache: #{data_id}"
|
18
|
+
end
|
19
|
+
|
20
|
+
# type --> :room
|
21
|
+
# class --> Room
|
22
|
+
# dir --> 'rooms'
|
23
|
+
def type_to_class(type)
|
24
|
+
type.to_s.capitalize.constantize
|
25
|
+
end
|
26
|
+
|
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
|
+
|
32
|
+
def type_to_dir(type)
|
33
|
+
type.to_s + 's'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module CacheDriver
|
2
|
+
class Config
|
3
|
+
attr_accessor :store
|
4
|
+
end
|
5
|
+
|
6
|
+
@@config = Config.new
|
7
|
+
|
8
|
+
def self.configed?
|
9
|
+
@@config.store
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.store_file?
|
13
|
+
@@config.store == :file
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.store_redis?
|
17
|
+
false
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.setup
|
21
|
+
yield @@config
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
class FileCacheUtil < CacheUtil
|
2
|
+
CACHE_DIR = Pathname.new(File.expand_path "../../../tmp/cache", __FILE__)
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def get_count(type)
|
6
|
+
dir = CACHE_DIR.join type_to_dir(type)
|
7
|
+
Dir.mkdir dir unless Dir.exist? dir
|
8
|
+
|
9
|
+
current_id = 0;
|
10
|
+
if File.exist? dir.join('count')
|
11
|
+
file = File.new dir.join('count'), 'r'
|
12
|
+
current_id = file.read.to_i
|
13
|
+
file.close
|
14
|
+
end
|
15
|
+
|
16
|
+
current_id
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_count(type, count)
|
20
|
+
dir = CACHE_DIR.join type_to_dir(type)
|
21
|
+
Dir.mkdir dir unless Dir.exist? dir
|
22
|
+
|
23
|
+
file = File.new dir.join('count'), 'w'
|
24
|
+
file.puts count
|
25
|
+
file.close
|
26
|
+
end
|
27
|
+
|
28
|
+
def write(type, data)
|
29
|
+
super type, data
|
30
|
+
|
31
|
+
dir = CACHE_DIR.join type_to_dir(type)
|
32
|
+
Dir.mkdir dir unless Dir.exist? dir
|
33
|
+
|
34
|
+
current_id = get_count(type)
|
35
|
+
if data.id
|
36
|
+
if data.id > current_id
|
37
|
+
raise "ID out of bounds"
|
38
|
+
end
|
39
|
+
else
|
40
|
+
current_id += 1
|
41
|
+
data.id = current_id
|
42
|
+
set_count(type, current_id)
|
43
|
+
end
|
44
|
+
file = File.new dir.join("#{data.id}.cache"), 'w'
|
45
|
+
file.puts "#{Time.now}\t#{data.to_cache}"
|
46
|
+
file.close
|
47
|
+
|
48
|
+
data.id
|
49
|
+
end
|
50
|
+
|
51
|
+
def read(type, data_id)
|
52
|
+
super type, data_id
|
53
|
+
|
54
|
+
dir = CACHE_DIR.join type_to_dir(type)
|
55
|
+
Dir.mkdir dir unless Dir.exist? dir
|
56
|
+
|
57
|
+
file_path = dir.join("#{data_id}.cache")
|
58
|
+
return unless File.exist? file_path
|
59
|
+
file = File.new file_path, 'r'
|
60
|
+
data_str = file.read.split("\t")[1]
|
61
|
+
|
62
|
+
unless data_str
|
63
|
+
puts "cache #{type}-#{data_id} data miss"
|
64
|
+
return
|
65
|
+
end
|
66
|
+
|
67
|
+
type_to_class(type).from_cache data_str
|
68
|
+
end
|
69
|
+
|
70
|
+
def read_all(type)
|
71
|
+
super type
|
72
|
+
|
73
|
+
dir = CACHE_DIR.join type_to_dir(type)
|
74
|
+
Dir.mkdir dir unless Dir.exist? dir
|
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
|
106
|
+
end
|
data/tmp/cache/empty
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cache_driver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- goshan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: make rails model act as ActiveRecord, but not save data into database
|
42
|
+
but cache system, file or redis
|
43
|
+
email:
|
44
|
+
- goshan.hanqiu@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- cache_driver.gemspec
|
53
|
+
- lib/cache_driver.rb
|
54
|
+
- lib/cache_driver/cache_record.rb
|
55
|
+
- lib/cache_driver/cache_util.rb
|
56
|
+
- lib/cache_driver/config.rb
|
57
|
+
- lib/cache_driver/file_cache_util.rb
|
58
|
+
- lib/cache_driver/version.rb
|
59
|
+
- tmp/cache/empty
|
60
|
+
homepage: https://github.com/goshan/cache_driver
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.4.5
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: A cache adapter for model accepting file or redis
|
84
|
+
test_files: []
|