Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
redis-objects-daily-counter
Advanced tools
This is a gem which extends Redis::Objects gem. Once install this gem, you can use the periodical counter, the periodical set, etc. in addition to the standard features of Redis::Objects. These counters and sets are useful for measuring conversions, implementing API rate limiting, MAU, DAU, and more.
Add this line to your application's Gemfile:
gem 'redis-objects-periodical'
If you want to know about installation and standard usage, please see Redis::Objects' GitHub page.
daily_counter
and daily_set
automatically creates keys that are unique to each object, in the format:
model_name:id:field_name:yyyy-mm-dd
I recommend using with expireat
option.
For illustration purposes, consider this stub class:
class Homepage
include Redis::Objects
daily_counter :pv, expireat: -> { Time.now + 2_678_400 } # about a month
daily_hash_key :browsing_history, expireat: -> { Time.now + 2_678_400 } # about a month
daily_set :dau, expireat: -> { Time.now + 2_678_400 } # about a month
def id
1
end
end
# 2021-04-01
homepage = Homepage.new
homepage.id # 1
homepage.pv.increment
homepage.pv.increment
homepage.pv.increment
puts homepage.pv.value # 3
# 2021-04-02 (next day)
puts homepage.pv.value # 0
homepage.pv.increment
homepage.pv.increment
puts homepage.pv.value # 2
start_date = Date.new(2021, 4, 1)
end_date = Date.new(2021, 4, 2)
homepage.pv.range(start_date, end_date) # [3, 2]
The periodical counters automatically switches the save destination when the date changes. You can access past dates counted values like Ruby arrays:
# 2021-04-01
homepage.pv.increment(3)
# 2021-04-02 (next day)
homepage.pv.increment(2)
# 2021-04-03 (next day)
homepage.pv.increment(5)
homepage.pv[Date.new(2021, 4, 1)] # => 3
homepage.pv[Date.new(2021, 4, 1), 3] # => [3, 2, 5]
homepage.pv[Date.new(2021, 4, 1)..Date.new(2021, 4, 2)] # => [3, 2]
homepage.pv.delete_at(Date.new(2021, 4, 1))
homepage.pv.range(Date.new(2021, 4, 1), Date.new(2021, 4, 3)) # => [0, 2, 5]
homepage.pv.at(Date.new(2021, 4, 2)) # => #<Redis::Counter key="homepage:1:pv:2021-04-02">
homepage.pv.at(Date.new(2021, 4, 2)).value # 2
annual_counter
model_name:id:field_name:yyyy
monthly_counter
model_name:id:field_name:yyyy-mm
weekly_counter
model_name:id:field_name:yyyyWw
daily_counter
model_name:id:field_name:yyyy-mm-dd
hourly_counter
model_name:id:field_name:yyyy-mm-ddThh
minutely_counter
model_name:id:field_name:yyyy-mm-ddThh:mi
The periodical hashes also automatically switches the save destination when the date changes.
# 2021-04-01
homepage.browsing_history.incr('item1')
homepage.browsing_history.incr('item2')
homepage.browsing_history.incr('item2')
puts homepage.browsing_history.all # { 'item1' => '1', 'item2' => '2' }
# 2021-04-02 (next day)
puts homepage.browsing_history.all # {}
homepage.browsing_history.bulk_set('item1' => 3, 'item3' => 5)
puts homepage.browsing_history.all # { 'item1' => '3', 'item3' => '5' }
# 2021-04-03 (next day)
homepage.browsing_history.incr('item2')
homepage.browsing_history.incr('item4')
puts homepage.browsing_history.all # { 'item2' => '1', 'item4' => '1' }
homepage.browsing_history[Date.new(2021, 4, 1)] # => { 'item1' => '1', 'item2' => '2' }
homepage.browsing_history[Date.new(2021, 4, 1), 3] # => { 'item1' => '4', 'item2' => '3', 'item3' => '5', 'item4' => '1' }
homepage.browsing_history[Date.new(2021, 4, 1)..Date.new(2021, 4, 2)] # => { 'item1' => '4', 'item2' => '2', 'item3' => '5' }
homepage.browsing_history.delete_at(Date.new(2021, 4, 1))
homepage.browsing_history.range(Date.new(2021, 4, 1), Date.new(2021, 4, 3)) # => { 'item1' => '3', 'item2' => '1', 'item3' => '5', 'item4' => '1' }
homepage.browsing_history.at(Date.new(2021, 4, 2)) # => #<Redis::HashKey key="homepage:1:browsing_history:2021-04-02">
homepage.browsing_history.at(Date.new(2021, 4, 2)).all # { 'item1' => '3', 'item3' => '5' }
annual_hash_key
model_name:id:field_name:yyyy
monthly_hash_key
model_name:id:field_name:yyyy-mm
weekly_hash_key
model_name:id:field_name:yyyyWw
daily_hash_key
model_name:id:field_name:yyyy-mm-dd
hourly_hash_key
model_name:id:field_name:yyyy-mm-ddThh
minutely_hash_key
model_name:id:field_name:yyyy-mm-ddThh:mi
The periodical sets also automatically switches the save destination when the date changes.
# 2021-04-01
homepage.dau << 'user1'
homepage.dau << 'user2'
homepage.dau << 'user1' # dup ignored
puts homepage.dau.members # ['user1', 'user2']
puts homepage.dau.length # 2
puts homepage.dau.count # alias of #length
# 2021-04-02 (next day)
puts homepage.dau.members # []
homepage.dau.merge('user2', 'user3')
puts homepage.dau.members # ['user2', 'user3']
# 2021-04-03 (next day)
homepage.dau.merge('user4')
homepage.dau[Date.new(2021, 4, 1)] # => ['user1', 'user2']
homepage.dau[Date.new(2021, 4, 1), 3] # => ['user1', 'user2', 'user3', 'user4']
homepage.dau[Date.new(2021, 4, 1)..Date.new(2021, 4, 2)] # => ['user1', 'user2', 'user3']
homepage.dau.delete_at(Date.new(2021, 4, 1))
homepage.dau.range(Date.new(2021, 4, 1), Date.new(2021, 4, 3)) # => ['user2', 'user3', 'user4']
homepage.dau.at(Date.new(2021, 4, 2)) # => #<Redis::Set key="homepage:1:dau:2021-04-02">
homepage.dau.at(Date.new(2021, 4, 2)).members # ['user2', 'user3']
annual_set
model_name:id:field_name:yyyy
monthly_set
model_name:id:field_name:yyyy-mm
weekly_set
model_name:id:field_name:yyyyWw
daily_set
model_name:id:field_name:yyyy-mm-dd
hourly_set
model_name:id:field_name:yyyy-mm-ddThh
minutely_set
model_name:id:field_name:yyyy-mm-ddThh:mi
This gem follows Ruby process' time zone, but if you extends Time class by ActiveSupport (e.g. Time.current
), follows Rails process' timezone.
The development environment for this gem is configured with docker-compose. Please use the following command:
$ docker-compose up -d
$ docker-compose run --rm ruby bundle
$ docker-compose run --rm ruby rspec .
$ docker-compose run --rm ruby rubocop -a
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/redis-objects-periodical. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Redis::Objects::Daily::Counter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
FAQs
Unknown package
We found that redis-objects-daily-counter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.