counter-cache-credis 0.0.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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +1 -0
- data/counter-cache-credis.gemspec +23 -0
- data/lib/counter/cache/credis.rb +3 -0
- data/lib/counter/cache/credis/counter.rb +56 -0
- data/lib/counter/cache/credis/redis_cli.rb +23 -0
- data/lib/counter/cache/credis/version.rb +7 -0
- data/lib/generators/redis_config_generator.rb +28 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a585b8d20156eb0c31c740f10e957f31774f0176
|
4
|
+
data.tar.gz: dfec7c1f05fab6df3d672ecbbbc2110aad33f96e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3cc2d71758aa92469216577aaac9c5dceec3dcbaa80fcec0947b536064c4f055bce903a760eece37c40f7c52ff66eccd118bb7643b51b5bbd10b311ce4c1e59a
|
7
|
+
data.tar.gz: d1ea1e9e42a5b77628a2106206e82abcc3fd8ab1b3f9bf968408d50ae802dbc67671b4b54c944b0eb4c3f60e78ef0e32108ef9a7519da22bb7e92f0b0fa09887
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 linjunzhu
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Counter::Cache::Redis
|
2
|
+
|
3
|
+
未完成。
|
4
|
+
|
5
|
+
实现效果:
|
6
|
+
|
7
|
+
能根据不同表来设置不同字段,读取也在 redis,而不用再通过 DB
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'counter-cache-redis'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install counter-cache-redis
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
TODO: Write usage instructions here
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it ( https://github.com/[my-github-username]/counter-cache-redis/fork )
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'counter/cache/credis/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "counter-cache-credis"
|
8
|
+
spec.version = Counter::Cache::Credis::VERSION
|
9
|
+
spec.authors = ["linjunzhu"]
|
10
|
+
spec.email = ["linjunzhugg@gmail.com"]
|
11
|
+
spec.summary = %q{将各种浏览量,点赞数之类的存于缓存}
|
12
|
+
spec.description = %q{将各种浏览量,点赞数之类的存于缓存,这样就不会每次都去 DB 修改}
|
13
|
+
spec.homepage = "http://linjunzhu.me"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Counter
|
2
|
+
module Cache
|
3
|
+
module Credis
|
4
|
+
|
5
|
+
def self.included(receiver)
|
6
|
+
receiver.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def defind_column_getter
|
12
|
+
self.class_eval do
|
13
|
+
define_method("get_#{self.column}_cache") do
|
14
|
+
self.send(self.column) + Redis.new.get("#{self.class.table_name}/#{self.id}").to_i
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def counter_cache_redis(options = {})
|
20
|
+
mattr_accessor :counter_delay, :column
|
21
|
+
self.counter_delay = options[:delay] || 1
|
22
|
+
self.column = options[:column] || 'views_count'
|
23
|
+
defind_column_getter if options[:column]
|
24
|
+
include Counter::Cache::Credis::InstanceMethods
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
module InstanceMethods
|
30
|
+
|
31
|
+
# 修改值
|
32
|
+
def update_counter
|
33
|
+
redis = RedisCli.new
|
34
|
+
# 先读出redis中的值
|
35
|
+
views_count_redis = redis.get("#{self.class.table_name}/#{self.id}").to_i
|
36
|
+
views_count_redis = 0 if !views_count_redis
|
37
|
+
views_count_redis += 1
|
38
|
+
|
39
|
+
if views_count_redis >= self.counter_delay
|
40
|
+
# 计算出总读数
|
41
|
+
views_count_temp = views_count_redis + (self.send(self.column) || 0)
|
42
|
+
views_count_redis = 0
|
43
|
+
self.send("#{self.column}=", views_count_temp)
|
44
|
+
self.save
|
45
|
+
end
|
46
|
+
|
47
|
+
redis.set("#{self.class.table_name}/#{self.id}", views_count_redis)
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
ActiveRecord::Base.send :include, Counter::Cache::Credis
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Counter
|
2
|
+
module Cache
|
3
|
+
module Credis
|
4
|
+
class RedisCli
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
config = YAML.load_file("./config/redis.yml")[Rails.env]
|
8
|
+
if config
|
9
|
+
@redis ||= Redis.new(:host => config['redis_host'], :port => config['redis_port'],
|
10
|
+
namespace: config['redis_namespace'], :db => config['redis_db'])
|
11
|
+
else
|
12
|
+
@redis ||= Redis.new
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(meth, *args, &blk)
|
17
|
+
@redis.send(meth, *args, &blk)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class RedisConfigGenerator < Rails::Generators::Base
|
2
|
+
def create_redis_file
|
3
|
+
file_name = 'config/redis.yml'
|
4
|
+
unless File.exists?(File.expand_path(file_name))
|
5
|
+
p "Creating #{file_name}"
|
6
|
+
File.open(file_name, 'w') do |f|
|
7
|
+
f.puts "redis: &redis"
|
8
|
+
f.puts " redis_port: 6379"
|
9
|
+
f.puts " redis_namespace: 'redis'"
|
10
|
+
f.puts " redis_db: 0"
|
11
|
+
f.puts ""
|
12
|
+
f.puts "test: "
|
13
|
+
f.puts " <<: *redis"
|
14
|
+
f.puts " redis_host: 'localhost'"
|
15
|
+
f.puts ""
|
16
|
+
f.puts "development: "
|
17
|
+
f.puts " <<: *redis"
|
18
|
+
f.puts " redis_host: 'localhost'"
|
19
|
+
f.puts ""
|
20
|
+
f.puts "production: "
|
21
|
+
f.puts " <<: *redis"
|
22
|
+
f.puts " redis_host: 'localhost'"
|
23
|
+
end
|
24
|
+
else
|
25
|
+
puts "Skipping #{file_name}, it already exists"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: counter-cache-credis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- linjunzhu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-20 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
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: 将各种浏览量,点赞数之类的存于缓存,这样就不会每次都去 DB 修改
|
42
|
+
email:
|
43
|
+
- linjunzhugg@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- counter-cache-credis.gemspec
|
54
|
+
- lib/counter/cache/credis.rb
|
55
|
+
- lib/counter/cache/credis/counter.rb
|
56
|
+
- lib/counter/cache/credis/redis_cli.rb
|
57
|
+
- lib/counter/cache/credis/version.rb
|
58
|
+
- lib/generators/redis_config_generator.rb
|
59
|
+
homepage: http://linjunzhu.me
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.4.3
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: 将各种浏览量,点赞数之类的存于缓存
|
83
|
+
test_files: []
|