counter-cache-credis 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/lib/counter/cache/credis/counter.rb +18 -10
- data/lib/counter/cache/credis/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32eb66fa3e40a126e33ffcc7baab164a975355d5
|
4
|
+
data.tar.gz: 4b3174e91c7ef491b84f871ba7495256a9b269ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b83c38ff786bd5090bf8a6657850913d31a5768881d6a4c331c23d6fc066927b0611e89846a5fcee8041f9a933ae3ddf2cf05580dd9b5e25f732536f21fb5b84
|
7
|
+
data.tar.gz: dd123be54e0ee6875ef223d64b75f3c5718848b32aed4dd916d8aec895c758810d3e7a789b7c3200cccb328a0f40c3feefe19ba0ca3bca6b2fb9e1ec214313ef
|
data/README.md
CHANGED
@@ -59,7 +59,9 @@ end
|
|
59
59
|
```ruby
|
60
60
|
student = Student.first
|
61
61
|
# It will increase itself by one
|
62
|
-
student.
|
62
|
+
student.increase_counter
|
63
|
+
# It will reduce itself by one
|
64
|
+
student.reduce_counter
|
63
65
|
# get views_count through redis
|
64
66
|
student.get_views_count_cache
|
65
67
|
```
|
@@ -30,24 +30,32 @@ module Counter
|
|
30
30
|
|
31
31
|
module InstanceMethods
|
32
32
|
|
33
|
-
#
|
34
|
-
def
|
33
|
+
# 增加
|
34
|
+
def increase_counter(column = 'views_count')
|
35
35
|
redis = RedisCli.new
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
if views_count_redis >= column_delay[column]
|
36
|
+
counter_redis = redis.get("#{self.class.table_name}/#{column}#{self.id}").to_i
|
37
|
+
counter_redis = 0 if !counter_redis
|
38
|
+
counter_redis += 1
|
39
|
+
if counter_redis >= column_delay[column]
|
41
40
|
# 计算出总读数
|
42
|
-
views_count_temp =
|
43
|
-
|
41
|
+
views_count_temp = counter_redis + (self.send(column) || 0)
|
42
|
+
counter_redis = 0
|
44
43
|
self.send("#{column}=", views_count_temp)
|
45
44
|
self.save
|
46
45
|
end
|
47
46
|
|
48
|
-
redis.set("#{self.class.table_name}/#{column}#{self.id}",
|
47
|
+
redis.set("#{self.class.table_name}/#{column}#{self.id}", counter_redis)
|
48
|
+
end
|
49
49
|
|
50
|
+
# 减低
|
51
|
+
def reduce_counter(column = 'views_count')
|
52
|
+
redis = RedisCli.new
|
53
|
+
counter_redis = redis.get("#{self.class.table_name}/#{column}#{self.id}").to_i
|
54
|
+
counter_redis = 0 if !counter_redis
|
55
|
+
counter_redis -= 1
|
56
|
+
redis.set("#{self.class.table_name}/#{column}#{self.id}", counter_redis)
|
50
57
|
end
|
58
|
+
|
51
59
|
end
|
52
60
|
end
|
53
61
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: counter-cache-credis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- linjunzhu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|