Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add :expires_in option support for RedisCacheStore increment/decrement method#33254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add :expires_in option support for RedisCacheStore increment/decremen…
…t method.
  • Loading branch information
@huacnlee
huacnlee committedJun 29, 2018
commit9d5b02ec5062a23665ec596ef7d3efe4f5abcc27
11 changes: 11 additions & 0 deletionsactivesupport/CHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -131,5 +131,16 @@

*Eileen M. Uchitelle*, *Aaron Patterson*

* RedisCacheStore: Support expiring counters.
Pass `expires_in: [seconds]` to `#increment` and `#decrement` options
to set the Redis EXPIRE if the counter doesn't exist.
If the counter exists, Redis doesn't extend its expiry when it's exist.

```
Rails.cache.increment("my_counter", 1, expires_in: 2.minutes)
```

*Jason Lee*


Please check [5-2-stable](https://github.com/rails/rails/blob/5-2-stable/activesupport/CHANGELOG.md) for previous changes.
24 changes: 22 additions & 2 deletionsactivesupport/lib/active_support/cache/redis_cache_store.rb
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -256,9 +256,19 @@ def delete_matched(matcher, options = nil)
#
# Failsafe: Raises errors.
def increment(name, amount = 1, options = nil)
options = merged_options(options)
key = normalize_key(name, options)
expires_in = options[:expires_in].to_i

instrument :increment, name, amount: amount do
failsafe :increment do
redis.with { |c| c.incrby normalize_key(name, options), amount }
redis.with do |c|
val = c.incrby key, amount
if expires_in > 0 && c.ttl(key) == -2
c.expire key, expires_in
end
val
end
end
end
end
Expand All@@ -272,9 +282,19 @@ def increment(name, amount = 1, options = nil)
#
# Failsafe: Raises errors.
def decrement(name, amount = 1, options = nil)
options = merged_options(options)
key = normalize_key(name, options)
expires_in = options[:expires_in].to_i

instrument :decrement, name, amount: amount do
failsafe :decrement do
redis.with { |c| c.decrby normalize_key(name, options), amount }
redis.with do |c|
val = c.decrby key, amount
if expires_in > 0 && c.ttl(key) == -2
c.expire key, expires_in
end
val
end
end
end
end
Expand Down
24 changes: 24 additions & 0 deletionsactivesupport/test/cache/stores/redis_cache_store_test.rb
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -141,6 +141,30 @@ def test_fetch_multi_uses_redis_mget
end
end
end

def test_increment_expires_in
assert_called_with @cache.redis, :incrby, [ "#{@namespace}:foo", 1 ] do
assert_called_with @cache.redis, :expire, [ "#{@namespace}:foo", 60 ] do
@cache.increment("foo", 1, expires_in: 60)
end
end

assert_not_called @cache.redis, :expire do
@cache.decrement("foo", 1, expires_in: 60)
end
end

def test_decrement_expires_in
assert_called_with @cache.redis, :decrby, [ "#{@namespace}:foo", 1 ] do
assert_called_with @cache.redis, :expire, [ "#{@namespace}:foo", 60 ] do
@cache.decrement("foo", 1, expires_in: 60)
end
end

assert_not_called @cache.redis, :expire do
@cache.decrement("foo", 1, expires_in: 60)
end
end
end

class ConnectionPoolBehaviourTest < StoreTest
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp