counter-cache-credis 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d461aa5d18a56b3b38020e4fa087d7ec54bb8fd
4
- data.tar.gz: 32bad92c4022621e8ee48ba4f5839ad9cc71be0d
3
+ metadata.gz: b26246e85681bfd50669fafcb38791677782facf
4
+ data.tar.gz: 5957a0dc5d94bc8e97c384fdca78b8b7e82a7a35
5
5
  SHA512:
6
- metadata.gz: d2d180f05fd184a127c03118a06b40ca9d49946a267af0a956ea0413442d3f7a5032b5c25ba16c6892fceb0c4e5d6f2edb45aff28fb6eda97329352608ba2469
7
- data.tar.gz: 40cfb7f6a2c5e0f7254a512ea8c4b632ca6404257b22609f1acd4b10263ca24fd414cf30743734880c8315b8d3b475cbe64875bde0b4d8e9f19ddab4dda673f4
6
+ metadata.gz: 967441b5d6ecbc769e5081c4c89029a36a16fd4014ae926d73e54b92dd57ea0978c33b88f0b55948c572a14141abb3ac2b8a0763a2fe2469f4086725ab4de9bf
7
+ data.tar.gz: 5c854609a04a1c1bdfe4e17790620a65b2599f63ecaffcbac73ad832d2ca22c8971936761fb519bf37e7895e4c6c99de4527a54cb397f56820980f580d80386c
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Counter::Cache::Redis
1
+ # Counter::Cache::CRedis
2
2
 
3
3
  Set a counter for model through redis in order to improve perfomance.
4
4
 
@@ -51,6 +51,11 @@ production:
51
51
 
52
52
  ### Use
53
53
 
54
+ ```ruby
55
+ class Student < ActiveRecord::Base
56
+ counter_cache_redis
57
+ end
58
+ ```
54
59
  ```ruby
55
60
  student = Student.first
56
61
  # It will increase itself by one
@@ -8,19 +8,21 @@ module Counter
8
8
 
9
9
  module ClassMethods
10
10
 
11
- def defind_column_getter
11
+ def defind_column_getter(column)
12
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
13
+ define_method("get_#{column}_cache") do
14
+ self.send(column) + Redis.new.get("#{self.class.table_name}/#{column}#{self.id}").to_i
15
15
  end
16
16
  end
17
17
  end
18
18
 
19
19
  def counter_cache_redis(options = {})
20
- mattr_accessor :counter_delay, :column
21
- self.counter_delay = options[:delay] || 20
22
- self.column = options[:column] || 'views_count'
23
- defind_column_getter if options[:column]
20
+ mattr_accessor :column_delay
21
+ delay = options[:delay] || 20
22
+ column = options[:column] || 'views_count'
23
+ self.column_delay = {} if self.column_delay.nil?
24
+ self.column_delay[column] = delay
25
+ defind_column_getter(column)
24
26
  include Counter::Cache::Credis::InstanceMethods
25
27
  end
26
28
 
@@ -29,22 +31,20 @@ module Counter
29
31
  module InstanceMethods
30
32
 
31
33
  # 修改值
32
- def update_counter
34
+ def update_counter(column = 'views_count')
33
35
  redis = RedisCli.new
34
- # 先读出redis中的值
35
- views_count_redis = redis.get("#{self.class.table_name}/#{self.id}").to_i
36
+ views_count_redis = redis.get("#{self.class.table_name}/#{column}#{self.id}").to_i
36
37
  views_count_redis = 0 if !views_count_redis
37
38
  views_count_redis += 1
38
-
39
- if views_count_redis >= self.counter_delay
39
+ if views_count_redis >= column_delay[column]
40
40
  # 计算出总读数
41
- views_count_temp = views_count_redis + (self.send(self.column) || 0)
41
+ views_count_temp = views_count_redis + (self.send(column) || 0)
42
42
  views_count_redis = 0
43
- self.send("#{self.column}=", views_count_temp)
43
+ self.send("#{column}=", views_count_temp)
44
44
  self.save
45
45
  end
46
46
 
47
- redis.set("#{self.class.table_name}/#{self.id}", views_count_redis)
47
+ redis.set("#{self.class.table_name}/#{column}#{self.id}", views_count_redis)
48
48
 
49
49
  end
50
50
  end
@@ -1,7 +1,7 @@
1
1
  module Counter
2
2
  module Cache
3
3
  module Credis
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter-cache-credis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - linjunzhu