counter

package
v1.1.40 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package counter supply counter service

Index

Constants

View Source
const (
	LUAFALSE int = 0
	LUATRUE  int = 1
)

Lua

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseEntity

type BaseEntity struct {
	ID    string `column:"id" pk:"Y" pkAuto:"n"`
	Value string `column:"val"`
}

BaseEntity is the base counter entity

func (*BaseEntity) Fields

func (p *BaseEntity) Fields() (Fields, error)

Fields implements EntityCounter.Fields

func (*BaseEntity) TableName

func (p *BaseEntity) TableName() string

TableName implements EntityInterface.TableName,it must be overrided or it will panic

func (*BaseEntity) ToBaseEntity

func (p *BaseEntity) ToBaseEntity(counterID string, fields Fields) (*BaseEntity, error)

ToBaseEntity convert counterID and fields to BaseEntity

func (*BaseEntity) ZeroFields

func (p *BaseEntity) ZeroFields() Fields

ZeroFields implements EntityCounter.ZeroFields,must be overrided

type Counter

type Counter interface {
	// GetName counter name
	GetName() string
	// Incr increase the counterID with fieldAndDelta
	Incr(counterID string, fieldAndDelta Fields) error

	// Get the fields of counterID
	Get(counterID string) (fields Fields, err error)

	// Del delete the counter whose id is `counterID“
	Del(counterID string) error
}

Counter service

type DBPersist

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

DBPersist implements Persist which persist counter to db

func NewDBPersist

func NewDBPersist(dbService func() orm.ShardDBService, entityType EntityCounter) (*DBPersist, error)

NewDBPersist create DBPersist

func (*DBPersist) Del

func (p *DBPersist) Del(counterID string) (deleted bool, err error)

Del implements Persist.Del

func (*DBPersist) Load

func (p *DBPersist) Load(counterID string) (fields Fields, err error)

Load implements Persist.Load

func (*DBPersist) Store

func (p *DBPersist) Store(counterID string, fields Fields) (err error)

Store implements Persist.Store

type EntityCounter

type EntityCounter interface {
	orm.Entity
	// Fields convert entity to Fields
	Fields() (Fields, error)
	// ZeroFields return zero fields,must not nil
	ZeroFields() Fields
	// Entity convert fields to EntityInterface
	Entity(counterID string, fields Fields) (orm.Entity, error)
}

EntityCounter entity counter

type Fields

type Fields map[string]int64

Fields define the counter's field and value

type NoPersistRedisCounter

type NoPersistRedisCounter struct {
	c.Initable
	Name string
	// contains filtered or unexported fields
}

NoPersistRedisCounter use redis implements Counter which not be pesisisted

func NewNoPersistRedisCounter

func NewNoPersistRedisCounter(name string, redisClient *cache.RedisClient, cacheParm *cache.ParamConf) (*NoPersistRedisCounter, error)

NewNoPersistRedisCounter create new NewNoPersistRedisCounter

func (*NoPersistRedisCounter) Del

func (p *NoPersistRedisCounter) Del(counterID string) error

Del implements Counter.Del

func (*NoPersistRedisCounter) DelFields

func (p *NoPersistRedisCounter) DelFields(counterID string, fields ...string) error

DelFields delete counter fileds of counterID

func (*NoPersistRedisCounter) Get

func (p *NoPersistRedisCounter) Get(counterID string) (fields Fields, err error)

Get implements Counter.Get

func (*NoPersistRedisCounter) GetName

func (p *NoPersistRedisCounter) GetName() string

GetName implements Counter.GetName

func (*NoPersistRedisCounter) Incr

func (p *NoPersistRedisCounter) Incr(counterID string, fieldAndDelta Fields) error

Incr implements Counter.Incr

func (*NoPersistRedisCounter) Init

func (p *NoPersistRedisCounter) Init() error

Init implements Initable.Init()

type Persist

type Persist interface {
	// Load the fields of counterID from persist storage
	Load(counterID string) (fields Fields, err error)

	// Del delete the counter whose id is `counterID“
	Del(counterID string) (deleted bool, err error)

	// Store save the value of fields with counterID
	Store(counterID string, fields Fields) error
}

Persist counter fields to the persist storage

type PersistRedisCounter

type PersistRedisCounter struct {
	c.Initable
	Name string
	// contains filtered or unexported fields
}

PersistRedisCounter use redis implements Counter which be pesisisted by `Persist“

func NewPersistRedisCounter

func NewPersistRedisCounter(name string, redisClientFunc func() *cache.RedisClient, scripts *Scripts, persist Persist, cacheParam *cache.ParamConf, slotsCount int) *PersistRedisCounter

NewPersistRedisCounter create RedisCounter service

func (*PersistRedisCounter) Del

func (p *PersistRedisCounter) Del(counterID string) (err error)

Del implements Counter.Del

func (*PersistRedisCounter) Get

func (p *PersistRedisCounter) Get(counterID string) (fields Fields, err error)

Get implements Counter.Get

func (*PersistRedisCounter) GetName

func (p *PersistRedisCounter) GetName() string

GetName implements Counter.GetName

func (*PersistRedisCounter) Incr

func (p *PersistRedisCounter) Incr(counterID string, fieldAndDelta Fields) error

Incr implements Counter.Incr

func (*PersistRedisCounter) Init

func (p *PersistRedisCounter) Init() error

Init implements Servcie.Init

type RedisCounterSync

type RedisCounterSync struct {
	Name string
	// contains filtered or unexported fields
}

RedisCounterSync sync redis counter to db

func NewRedisCounterSync

func NewRedisCounterSync(persistRedisCounter *PersistRedisCounter, slotMaxItems, minSyncVersionChanges, minSyncIntervalSecond, evictIntervalSecond int64) (*RedisCounterSync, error)

NewRedisCounterSync create new RedisCounterSync

func (*RedisCounterSync) ScanAll

func (p *RedisCounterSync) ScanAll() error

ScanAll scan all redis server counter sync set

func (*RedisCounterSync) Stop

func (p *RedisCounterSync) Stop()

Stop stop the scan task

type RedisCounterSyncSchedule

type RedisCounterSyncSchedule struct {
	c.BaseService
	// contains filtered or unexported fields
}

RedisCounterSyncSchedule schedul RedisCounterSync task

func NewRedisCounterSyncSchedule

func NewRedisCounterSyncSchedule(name string, redisCounterSync []*RedisCounterSync, scanIntervalSecond int) (*RedisCounterSyncSchedule, error)

NewRedisCounterSyncSchedule create new RedisCounterSyncSchedule

func (*RedisCounterSyncSchedule) Init

func (p *RedisCounterSyncSchedule) Init() error

Init implements Initable.Init

func (*RedisCounterSyncSchedule) Start

func (p *RedisCounterSyncSchedule) Start() bool

Start implements Servcie.Start()

func (*RedisCounterSyncSchedule) Stop

func (p *RedisCounterSyncSchedule) Stop() bool

Stop implements Service.Stop()

type Scripts

type Scripts struct {
	Update  string `yaml:"update_lua"`
	SetSync string `yaml:"sync_set_lua"`
	Evict   string `yaml:"evict_lua"`
	HgetAll string `yaml:"hgetall_lua"`
	Del     string `yaml:"del_lua"`
	// contains filtered or unexported fields
}

Scripts define persist counter lua scripts

func NewScripts

func NewScripts(loadFromString bool) *Scripts

NewScripts new

func (*Scripts) Init

func (p *Scripts) Init() (err error)

Init implements Init

Jump to

Keyboard shortcuts

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