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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0659026d81d19b19e71bbf5120cac135877778b2
4
- data.tar.gz: 5cb0f7319b034f61a185d24b29ef570662e8592c
3
+ metadata.gz: 32eb66fa3e40a126e33ffcc7baab164a975355d5
4
+ data.tar.gz: 4b3174e91c7ef491b84f871ba7495256a9b269ee
5
5
  SHA512:
6
- metadata.gz: 30978b4e3e69c1d6a339ebf0e8f628e316fc9bb0638d5d71004dcd508e9b0501664c3eb7f1297b1b9b8b41519e8ef3412cd1599c279f34fdc678b2fa57ec4a71
7
- data.tar.gz: 68931ac0e8975c3436bf66c7a466ff7787e319b3f6e27c3ad8e3548be8424e58a73ccff38001b34c8cd236da7f9dae7e33b7858e3de217305086b3bee7d5880e
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.update_counter
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 update_counter(column = 'views_count')
33
+ # 增加
34
+ def increase_counter(column = 'views_count')
35
35
  redis = RedisCli.new
36
- p redis
37
- views_count_redis = redis.get("#{self.class.table_name}/#{column}#{self.id}").to_i
38
- views_count_redis = 0 if !views_count_redis
39
- views_count_redis += 1
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 = views_count_redis + (self.send(column) || 0)
43
- views_count_redis = 0
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}", views_count_redis)
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
@@ -1,7 +1,7 @@
1
1
  module Counter
2
2
  module Cache
3
3
  module Credis
4
- VERSION = "0.0.4"
4
+ VERSION = "0.0.5"
5
5
  end
6
6
  end
7
7
  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
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-05-20 00:00:00.000000000 Z
11
+ date: 2015-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler