rubocop-redis 0.1.4 → 0.1.6
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 +4 -4
- data/README.md +2 -0
- data/config/default.yml +7 -0
- data/docs/modules/ROOT/pages/cops_redis.adoc +31 -2
- data/lib/rubocop/cop/redis/setex.rb +61 -0
- data/lib/rubocop/cop/redis_cops.rb +1 -0
- data/lib/rubocop/redis/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 819ed1dc44a32d761db56ef63fff4c104deab950463ab50eca791ae2c566b94e
|
4
|
+
data.tar.gz: d1934717d789352034216cda7d24d5d3daf4e27b33bf6e9b3e9eeb1c3efc91c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5dcee7ad178036bb28f08abf1d41834833471642aa4ed7e414052a20f694ca5d00a4fdd363f494565fef3051d70876aee9fae24b04510f26424ad5a4905d259e
|
7
|
+
data.tar.gz: 33b33abc9a8d42ba38752a7380dd48bb702aa9ccef3e00a8089f9f922073058d5813a0cd66f8090dd0dad53e0f1c130a0473baad0fa51c230c645ac744fc5707
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# rubocop-redis
|
2
2
|
|
3
|
+
[](https://github.com/9sako6/rubocop-redis/actions/workflows/test.yml)
|
4
|
+
|
3
5
|
An extension of RuboCop for [redis/redis-rb](https://github.com/redis/redis-rb) Redis client.
|
4
6
|
|
5
7
|
## Documentation
|
data/config/default.yml
CHANGED
@@ -4,3 +4,10 @@ Redis/NotKeys:
|
|
4
4
|
Description: "Do not use `.keys`. It may ruin performance when it is executed against large databases. See https://redis.io/commands/keys/ for details."
|
5
5
|
Enabled: true
|
6
6
|
VersionAdded: "0.1"
|
7
|
+
Safe: false
|
8
|
+
|
9
|
+
Redis/Setex:
|
10
|
+
Description: "Use `setex`"
|
11
|
+
Enabled: true
|
12
|
+
VersionAdded: "0.1"
|
13
|
+
Safe: true
|
@@ -6,7 +6,7 @@
|
|
6
6
|
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
|
7
7
|
|
8
8
|
| Enabled
|
9
|
-
|
|
9
|
+
| No
|
10
10
|
| No
|
11
11
|
| 0.1
|
12
12
|
| -
|
@@ -16,7 +16,7 @@ This cop detects the use of `KEYS` query that should be noted when used in a pro
|
|
16
16
|
Consider using `SCAN` query instead of `KEYS` query.
|
17
17
|
See https://redis.io/commands/keys/ for details.
|
18
18
|
|
19
|
-
To avoid detecting `Hash#keys` calling, this cop adds an offense if a receiver of `keys` method is named `redis`.
|
19
|
+
To avoid detecting `Hash#keys` calling, this cop adds an offense only if a receiver of `keys` method is named `redis`.
|
20
20
|
|
21
21
|
=== Examples
|
22
22
|
|
@@ -38,3 +38,32 @@ loop do
|
|
38
38
|
end
|
39
39
|
all_keys
|
40
40
|
----
|
41
|
+
|
42
|
+
== Redis/Setex
|
43
|
+
|
44
|
+
|===
|
45
|
+
| Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
|
46
|
+
|
47
|
+
| Enabled
|
48
|
+
| Yes
|
49
|
+
| No
|
50
|
+
| 0.1
|
51
|
+
| -
|
52
|
+
|===
|
53
|
+
|
54
|
+
Use `setex` method instead of calling both `set` and `expire` methods in a transaction.
|
55
|
+
See https://redis.io/commands/setex/ for details.
|
56
|
+
|
57
|
+
=== Examples
|
58
|
+
|
59
|
+
[source,ruby]
|
60
|
+
----
|
61
|
+
# bad
|
62
|
+
redis.multi do
|
63
|
+
redis.set(key, value)
|
64
|
+
redis.expire(key, 120)
|
65
|
+
end
|
66
|
+
|
67
|
+
# good
|
68
|
+
redis.setex(key, 120, value)
|
69
|
+
----
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Redis
|
6
|
+
# Use `setex` method instead of calling both `set` and `expire` methods in a transaction.
|
7
|
+
# See https://redis.io/commands/setex/ for details.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # bad
|
11
|
+
# redis.multi do
|
12
|
+
# redis.set(key, value)
|
13
|
+
# redis.expire(key, 120)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# redis.setex(key, 120, value)
|
18
|
+
#
|
19
|
+
class Setex < Base
|
20
|
+
MSG = "Use `setex` method instead of calling both `set` and `expire` methods in a transaction."
|
21
|
+
|
22
|
+
def_node_matcher :multi_block?, <<~PATTERN
|
23
|
+
(block $(send ... :multi) args (begin $...))
|
24
|
+
PATTERN
|
25
|
+
|
26
|
+
def_node_matcher :match_set_call?, <<~PATTERN
|
27
|
+
(send ($...) :set ...)
|
28
|
+
PATTERN
|
29
|
+
|
30
|
+
def_node_matcher :match_expire_call?, <<~PATTERN
|
31
|
+
(send ($...) :expire ...)
|
32
|
+
PATTERN
|
33
|
+
|
34
|
+
RESTRICT_ON_SEND = %i[sets expire].freeze
|
35
|
+
|
36
|
+
def on_block(node)
|
37
|
+
return unless multi_block?(node)
|
38
|
+
return unless match_set_and_expire_in_multi_block?(node)
|
39
|
+
|
40
|
+
add_offense(node)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def match_set_and_expire_in_multi_block?(node)
|
46
|
+
_, statements = multi_block?(node)
|
47
|
+
result = false
|
48
|
+
called_set = false
|
49
|
+
|
50
|
+
statements.each do |statement|
|
51
|
+
called_set = true if match_set_call?(statement)
|
52
|
+
|
53
|
+
result = true if called_set && match_expire_call?(statement)
|
54
|
+
end
|
55
|
+
|
56
|
+
result
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-redis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 9sako6
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-09-
|
11
|
+
date: 2022-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: An extension of RuboCop for redis/redis gem.
|
14
14
|
email:
|
@@ -28,6 +28,7 @@ files:
|
|
28
28
|
- docs/modules/ROOT/pages/cops_redis.adoc
|
29
29
|
- lib/rubocop-redis.rb
|
30
30
|
- lib/rubocop/cop/redis/not_keys.rb
|
31
|
+
- lib/rubocop/cop/redis/setex.rb
|
31
32
|
- lib/rubocop/cop/redis_cops.rb
|
32
33
|
- lib/rubocop/redis.rb
|
33
34
|
- lib/rubocop/redis/inject.rb
|