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 +4 -4
- data/README.md +6 -1
- data/lib/counter/cache/credis/counter.rb +15 -15
- data/lib/counter/cache/credis/version.rb +1 -1
- 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: b26246e85681bfd50669fafcb38791677782facf
|
4
|
+
data.tar.gz: 5957a0dc5d94bc8e97c384fdca78b8b7e82a7a35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 967441b5d6ecbc769e5081c4c89029a36a16fd4014ae926d73e54b92dd57ea0978c33b88f0b55948c572a14141abb3ac2b8a0763a2fe2469f4086725ab4de9bf
|
7
|
+
data.tar.gz: 5c854609a04a1c1bdfe4e17790620a65b2599f63ecaffcbac73ad832d2ca22c8971936761fb519bf37e7895e4c6c99de4527a54cb397f56820980f580d80386c
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Counter::Cache::
|
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_#{
|
14
|
-
self.send(
|
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 :
|
21
|
-
|
22
|
-
|
23
|
-
|
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
|
-
|
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(
|
41
|
+
views_count_temp = views_count_redis + (self.send(column) || 0)
|
42
42
|
views_count_redis = 0
|
43
|
-
self.send("#{
|
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
|