timebased

package module
v0.0.0-...-ea027da Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2022 License: BSD-3-Clause Imports: 6 Imported by: 0

README

Timebased

This repo contains useful methods can be used in your daily development.

Timebased stats for a period of time.

You want to know most viewed products within 3 hours. Assume we have 3 buckets to store recent 3 hours views into them, here is a list

Hour 1:00 Hour 2:00 Hour 3:00
P1: 0 P1: 13 p1: 9
P2: 5 P2: 11 p2: 29
P3: 7 P3: 0 p3: 0
P4: 0 P4: 0 p4: 21

Now, we are at fourth hour, (assume it's 3:10), we can get a rank list at the end.

  1. P2: 45
  2. p1: 22
  3. p4: 21
  4. p3: 7

You can check the implementation under examples/.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlignToInterval

func AlignToInterval(interval time.Duration) <-chan time.Time

Types

type Element

type Element struct {
	Key   string
	Count uint64
}

type MemStore

type MemStore struct {
	// contains filtered or unexported fields
}

MemStore maintains a list of counter, when an interval passes, it rotates. The oldest (pointed by current) will be cleared and start a new counting process.

func NewMemStore

func NewMemStore(interval time.Duration, cap int) *MemStore

type RedisStore

type RedisStore struct {
	// contains filtered or unexported fields
}

func NewRedisStore

func NewRedisStore(interval time.Duration, cap int, name string, conn redis.Conn) *RedisStore

type Store

type Store interface {
	// contains filtered or unexported methods
}

type TimedCounter

type TimedCounter struct {
	// contains filtered or unexported fields
}

TimedCounter every interval passes, try to store and reset the counter.

func NewCounterWithStore

func NewCounterWithStore(interval time.Duration, store Store) *TimedCounter

func (*TimedCounter) Inc

func (tc *TimedCounter) Inc(key string)

func (*TimedCounter) Report

func (tc *TimedCounter) Report() (result []Element, err error)

func (*TimedCounter) Stop

func (tc *TimedCounter) Stop()

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL