cache

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: Apache-2.0 Imports: 42 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NonPersonalized                  = "non-personalized"
	NonPersonalizedDigest            = "non-personalized_digest"
	NonPersonalizedUpdateTime        = "non-personalized_update_time"
	ItemToItem                       = "item-to-item"
	ItemToItemDigest                 = "item-to-item_digest"
	ItemToItemUpdateTime             = "item-to-item_update_time"
	UserToUser                       = "user-to-user"
	UserToUserDigest                 = "user-to-user_digest"
	UserToUserUpdateTime             = "user-to-user_update_time"
	CollaborativeFiltering           = "collaborative-filtering"
	CollaborativeFilteringDigest     = "collaborative-filtering_digest"
	CollaborativeFilteringUpdateTime = "collaborative-filtering_update_time"
	Recommend                        = "recommend"
	RecommendDigest                  = "recommend_digest"
	RecommendUpdateTime              = "recommend_update_time"

	// ItemCategories is the set of item categories. The format of key:
	//	Global item categories - item_categories
	ItemCategories = "item_categories"

	LastModifyItemTime = "last_modify_item_time" // the latest timestamp that a user related data was modified
	LastModifyUserTime = "last_modify_user_time" // the latest timestamp that an item related data was modified

	// GlobalMeta is global meta information
	GlobalMeta                 = "global_meta"
	DataImported               = "data_imported"
	NumUsers                   = "num_users"
	NumItems                   = "num_items"
	NumFeedback                = "num_feedback"
	NumPosFeedbacks            = "num_pos_feedbacks"
	NumNegFeedbacks            = "num_neg_feedbacks"
	NumUserLabels              = "num_user_labels"
	NumItemLabels              = "num_item_labels"
	NumTotalPosFeedbacks       = "num_total_pos_feedbacks"
	NumValidPosFeedbacks       = "num_valid_pos_feedbacks"
	NumValidNegFeedbacks       = "num_valid_neg_feedbacks"
	LastFitMatchingModelTime   = "last_fit_matching_model_time"
	LastFitRankingModelTime    = "last_fit_ranking_model_time"
	LastUpdateLatestItemsTime  = "last_update_latest_items_time"  // the latest timestamp that latest items were updated
	LastUpdatePopularItemsTime = "last_update_popular_items_time" // the latest timestamp that popular items were updated
	CFNDCG                     = "cf_ndcg"
	CFPrecision                = "cf_precision"
	CFRecall                   = "cf_recall"
	CTRPrecision               = "ctr_precision"
	CTRRecall                  = "ctr_recall"
	CTRAUC                     = "ctr_auc"
	PositiveFeedbackRatio      = "positive_feedback_ratio"
)

Variables

View Source
var (
	ErrObjectNotExist = errors.NotFoundf("object")
	ErrNoDatabase     = errors.NotAssignedf("database")
)

Functions

func ConvertDocumentsToValues

func ConvertDocumentsToValues(documents []Score) []string

func Key

func Key(keys ...string) string

Key creates key for cache. Empty field will be ignored.

func Register

func Register(prefixes []string, creator Creator)

Register a database creator.

func SortDocuments

func SortDocuments(documents []Score)

Types

type Creator

type Creator func(path, tablePrefix string, opts ...storage.Option) (Database, error)

Creator creates a database instance.

type Database

type Database interface {
	Close() error
	Ping() error
	Init() error
	Scan(work func(string) error) error
	Purge() error

	Set(ctx context.Context, values ...Value) error
	Get(ctx context.Context, name string) *ReturnValue
	Delete(ctx context.Context, name string) error

	Push(ctx context.Context, name, value string) error
	Pop(ctx context.Context, name string) (string, error)
	Remain(ctx context.Context, name string) (int64, error)

	AddScores(ctx context.Context, collection, subset string, documents []Score) error
	SearchScores(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Score, error)
	DeleteScores(ctx context.Context, collection []string, condition ScoreCondition) error
	UpdateScores(ctx context.Context, collections []string, subset *string, id string, patch ScorePatch) error
	ScanScores(ctx context.Context, callback func(collection, id, subset string, timestamp time.Time) error) error

	AddTimeSeriesPoints(ctx context.Context, points []TimeSeriesPoint) error
	GetTimeSeriesPoints(ctx context.Context, name string, begin, end time.Time, duration time.Duration) ([]TimeSeriesPoint, error)
}

Database is the common interface for cache store.

func Open

func Open(path, tablePrefix string, opts ...storage.Option) (Database, error)

Open a connection to a database.

type Message

type Message struct {
	Name      string `gorm:"primaryKey;index:timestamp"`
	Value     string `gorm:"primaryKey"`
	Timestamp int64  `gorm:"index:timestamp"`
}

type MongoDB

type MongoDB struct {
	storage.TablePrefix
	// contains filtered or unexported fields
}

func (MongoDB) AddScores

func (m MongoDB) AddScores(ctx context.Context, collection, subset string, documents []Score) error

func (MongoDB) AddTimeSeriesPoints

func (m MongoDB) AddTimeSeriesPoints(ctx context.Context, points []TimeSeriesPoint) error

func (MongoDB) Close

func (m MongoDB) Close() error

func (MongoDB) Delete

func (m MongoDB) Delete(ctx context.Context, name string) error

func (MongoDB) DeleteScores

func (m MongoDB) DeleteScores(ctx context.Context, collections []string, condition ScoreCondition) error

func (MongoDB) Get

func (m MongoDB) Get(ctx context.Context, name string) *ReturnValue

func (MongoDB) GetTimeSeriesPoints

func (m MongoDB) GetTimeSeriesPoints(ctx context.Context, name string, begin, end time.Time, duration time.Duration) ([]TimeSeriesPoint, error)

func (MongoDB) Init

func (m MongoDB) Init() error

func (MongoDB) Ping

func (m MongoDB) Ping() error

func (MongoDB) Pop

func (m MongoDB) Pop(ctx context.Context, name string) (string, error)

func (MongoDB) Purge

func (m MongoDB) Purge() error

func (MongoDB) Push

func (m MongoDB) Push(ctx context.Context, name, value string) error

func (MongoDB) Remain

func (m MongoDB) Remain(ctx context.Context, name string) (int64, error)

func (MongoDB) Scan

func (m MongoDB) Scan(work func(string) error) error

func (MongoDB) ScanScores

func (m MongoDB) ScanScores(ctx context.Context, callback func(collection string, id string, subset string, timestamp time.Time) error) error

func (MongoDB) SearchScores

func (m MongoDB) SearchScores(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Score, error)

func (MongoDB) Set

func (m MongoDB) Set(ctx context.Context, values ...Value) error

func (MongoDB) UpdateScores

func (m MongoDB) UpdateScores(ctx context.Context, collections []string, subset *string, id string, patch ScorePatch) error

type NoDatabase

type NoDatabase struct{}

NoDatabase means no database used for cache.

func (NoDatabase) AddScores

func (NoDatabase) AddScores(_ context.Context, _, _ string, _ []Score) error

func (NoDatabase) AddTimeSeriesPoints

func (NoDatabase) AddTimeSeriesPoints(_ context.Context, _ []TimeSeriesPoint) error

func (NoDatabase) Close

func (NoDatabase) Close() error

Close method of NoDatabase returns ErrNoDatabase.

func (NoDatabase) Delete

func (NoDatabase) Delete(_ context.Context, _ string) error

Delete method of NoDatabase returns ErrNoDatabase.

func (NoDatabase) DeleteScores

func (NoDatabase) DeleteScores(_ context.Context, _ []string, _ ScoreCondition) error

func (NoDatabase) Get

Get method of NoDatabase returns ErrNoDatabase.

func (NoDatabase) GetTimeSeriesPoints

func (NoDatabase) GetTimeSeriesPoints(_ context.Context, _ string, _, _ time.Time, _ time.Duration) ([]TimeSeriesPoint, error)

func (NoDatabase) Init

func (NoDatabase) Init() error

Init method of NoDatabase returns ErrNoDatabase.

func (NoDatabase) Ping

func (NoDatabase) Ping() error

func (NoDatabase) Pop

func (NoDatabase) Purge

func (NoDatabase) Purge() error

func (NoDatabase) Push

func (NoDatabase) Push(_ context.Context, _, _ string) error

func (NoDatabase) Remain

func (NoDatabase) Remain(_ context.Context, _ string) (int64, error)

func (NoDatabase) Scan

func (NoDatabase) Scan(_ func(string) error) error

func (NoDatabase) ScanScores

func (NoDatabase) ScanScores(context.Context, func(collection, id, subset string, timestamp time.Time) error) error

func (NoDatabase) SearchScores

func (NoDatabase) SearchScores(_ context.Context, _, _ string, _ []string, _, _ int) ([]Score, error)

func (NoDatabase) Set

func (NoDatabase) Set(_ context.Context, _ ...Value) error

func (NoDatabase) UpdateScores

type PostgresDocument

type PostgresDocument struct {
	Collection string `gorm:"primaryKey"`
	Subset     string `gorm:"primaryKey"`
	Id         string `gorm:"primaryKey"`
	IsHidden   bool
	Categories pq.StringArray `gorm:"type:text[]"`
	Score      float64
	Timestamp  time.Time
}

type ProxyClient

type ProxyClient struct {
	protocol.CacheStoreClient
}

func NewProxyClient

func NewProxyClient(conn *grpc.ClientConn) *ProxyClient

func (ProxyClient) AddScores

func (p ProxyClient) AddScores(ctx context.Context, collection, subset string, documents []Score) error

func (ProxyClient) AddTimeSeriesPoints

func (p ProxyClient) AddTimeSeriesPoints(ctx context.Context, points []TimeSeriesPoint) error

func (ProxyClient) Close

func (p ProxyClient) Close() error

func (ProxyClient) Delete

func (p ProxyClient) Delete(ctx context.Context, name string) error

func (ProxyClient) DeleteScores

func (p ProxyClient) DeleteScores(ctx context.Context, collection []string, condition ScoreCondition) error

func (ProxyClient) Get

func (p ProxyClient) Get(ctx context.Context, name string) *ReturnValue

func (ProxyClient) GetTimeSeriesPoints

func (p ProxyClient) GetTimeSeriesPoints(ctx context.Context, name string, begin, end time.Time, duration time.Duration) ([]TimeSeriesPoint, error)

func (ProxyClient) Init

func (p ProxyClient) Init() error

func (ProxyClient) Ping

func (p ProxyClient) Ping() error

func (ProxyClient) Pop

func (p ProxyClient) Pop(ctx context.Context, name string) (string, error)

func (ProxyClient) Purge

func (p ProxyClient) Purge() error

func (ProxyClient) Push

func (p ProxyClient) Push(ctx context.Context, name, value string) error

func (ProxyClient) Remain

func (p ProxyClient) Remain(ctx context.Context, name string) (int64, error)

func (ProxyClient) Scan

func (p ProxyClient) Scan(_ func(string) error) error

func (ProxyClient) ScanScores

func (p ProxyClient) ScanScores(ctx context.Context, callback func(collection string, id string, subset string, timestamp time.Time) error) error

func (ProxyClient) SearchScores

func (p ProxyClient) SearchScores(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Score, error)

func (ProxyClient) Set

func (p ProxyClient) Set(ctx context.Context, values ...Value) error

func (ProxyClient) UpdateScores

func (p ProxyClient) UpdateScores(ctx context.Context, collection []string, subset *string, id string, patch ScorePatch) error

type ProxyServer

type ProxyServer struct {
	protocol.UnimplementedCacheStoreServer
	// contains filtered or unexported fields
}

func NewProxyServer

func NewProxyServer(database Database) *ProxyServer

func (*ProxyServer) AddScores

func (*ProxyServer) AddTimeSeriesPoints

func (*ProxyServer) Delete

func (*ProxyServer) DeleteScores

func (*ProxyServer) Get

func (*ProxyServer) GetTimeSeriesPoints

func (*ProxyServer) Ping

func (*ProxyServer) Pop

func (*ProxyServer) Push

func (*ProxyServer) Remain

func (*ProxyServer) ScanScores

func (*ProxyServer) SearchScores

func (*ProxyServer) Serve

func (p *ProxyServer) Serve(lis net.Listener) error

func (*ProxyServer) Set

func (*ProxyServer) Stop

func (p *ProxyServer) Stop()

func (*ProxyServer) UpdateScores

type Redis

type Redis struct {
	storage.TablePrefix
	// contains filtered or unexported fields
}

Redis cache storage.

func (*Redis) AddScores

func (r *Redis) AddScores(ctx context.Context, collection, subset string, documents []Score) error

func (*Redis) AddTimeSeriesPoints

func (r *Redis) AddTimeSeriesPoints(ctx context.Context, points []TimeSeriesPoint) error

func (*Redis) Close

func (r *Redis) Close() error

Close redis connection.

func (*Redis) Delete

func (r *Redis) Delete(ctx context.Context, key string) error

Delete object from Redis.

func (*Redis) DeleteScores

func (r *Redis) DeleteScores(ctx context.Context, collections []string, condition ScoreCondition) error

func (*Redis) Get

func (r *Redis) Get(ctx context.Context, key string) *ReturnValue

Get returns a value from Redis.

func (*Redis) GetTimeSeriesPoints

func (r *Redis) GetTimeSeriesPoints(ctx context.Context, name string, begin, end time.Time, duration time.Duration) ([]TimeSeriesPoint, error)

func (*Redis) Init

func (r *Redis) Init() error

Init nothing.

func (*Redis) Ping

func (r *Redis) Ping() error

func (*Redis) Pop

func (r *Redis) Pop(ctx context.Context, name string) (string, error)

func (*Redis) Purge

func (r *Redis) Purge() error

func (*Redis) Push

func (r *Redis) Push(ctx context.Context, name string, message string) error

func (*Redis) Remain

func (r *Redis) Remain(ctx context.Context, name string) (int64, error)

func (*Redis) Scan

func (r *Redis) Scan(work func(string) error) error

func (*Redis) ScanScores

func (r *Redis) ScanScores(ctx context.Context, callback func(collection string, id string, subset string, timestamp time.Time) error) error

func (*Redis) SearchScores

func (r *Redis) SearchScores(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Score, error)

func (*Redis) Set

func (r *Redis) Set(ctx context.Context, values ...Value) error

func (*Redis) UpdateScores

func (r *Redis) UpdateScores(ctx context.Context, collections []string, subset *string, id string, patch ScorePatch) error

type ReturnValue

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

func (*ReturnValue) Exists

func (r *ReturnValue) Exists() bool

func (*ReturnValue) Integer

func (r *ReturnValue) Integer() (int, error)

func (*ReturnValue) String

func (r *ReturnValue) String() (string, error)

func (*ReturnValue) Time

func (r *ReturnValue) Time() (time.Time, error)

type SQLDatabase

type SQLDatabase struct {
	storage.TablePrefix
	// contains filtered or unexported fields
}

func (*SQLDatabase) AddScores

func (db *SQLDatabase) AddScores(ctx context.Context, collection, subset string, documents []Score) error

func (*SQLDatabase) AddTimeSeriesPoints

func (db *SQLDatabase) AddTimeSeriesPoints(ctx context.Context, points []TimeSeriesPoint) error

func (*SQLDatabase) Close

func (db *SQLDatabase) Close() error

func (*SQLDatabase) Delete

func (db *SQLDatabase) Delete(ctx context.Context, name string) error

func (*SQLDatabase) DeleteScores

func (db *SQLDatabase) DeleteScores(ctx context.Context, collections []string, condition ScoreCondition) error

func (*SQLDatabase) Get

func (db *SQLDatabase) Get(ctx context.Context, name string) *ReturnValue

func (*SQLDatabase) GetTimeSeriesPoints

func (db *SQLDatabase) GetTimeSeriesPoints(ctx context.Context, name string, begin, end time.Time, duration time.Duration) ([]TimeSeriesPoint, error)

func (*SQLDatabase) Init

func (db *SQLDatabase) Init() error

func (*SQLDatabase) Ping

func (db *SQLDatabase) Ping() error

func (*SQLDatabase) Pop

func (db *SQLDatabase) Pop(ctx context.Context, name string) (string, error)

func (*SQLDatabase) Purge

func (db *SQLDatabase) Purge() error

func (*SQLDatabase) Push

func (db *SQLDatabase) Push(ctx context.Context, name, value string) error

func (*SQLDatabase) Remain

func (db *SQLDatabase) Remain(ctx context.Context, name string) (count int64, err error)

func (*SQLDatabase) Scan

func (db *SQLDatabase) Scan(work func(string) error) error

func (*SQLDatabase) ScanScores

func (db *SQLDatabase) ScanScores(ctx context.Context, callback func(collection, id, subset string, timestamp time.Time) error) error

func (*SQLDatabase) SearchScores

func (db *SQLDatabase) SearchScores(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Score, error)

func (*SQLDatabase) Set

func (db *SQLDatabase) Set(ctx context.Context, values ...Value) error

func (*SQLDatabase) UpdateScores

func (db *SQLDatabase) UpdateScores(ctx context.Context, collections []string, subset *string, id string, patch ScorePatch) error

type SQLDocument

type SQLDocument struct {
	Collection string `gorm:"primaryKey"`
	Subset     string `gorm:"primaryKey"`
	Id         string `gorm:"primaryKey"`
	IsHidden   bool
	Categories []string `gorm:"type:text;serializer:json"`
	Score      float64
	Timestamp  time.Time
}

type SQLDriver

type SQLDriver int
const (
	MySQL SQLDriver = iota
	Postgres
	SQLite
)

type SQLValue

type SQLValue struct {
	Name  string `gorm:"type:varchar(256);primaryKey"`
	Value string `gorm:"type:varchar(256);not null"`
}

type Score

type Score struct {
	Id         string
	Score      float64
	IsHidden   bool      `json:"-"`
	Categories []string  `json:"-" gorm:"type:text;serializer:json"`
	Timestamp  time.Time `json:"-"`
}

type ScoreCondition

type ScoreCondition struct {
	Subset *string
	Id     *string
	Before *time.Time
}

func (*ScoreCondition) Check

func (condition *ScoreCondition) Check() error

type ScorePatch

type ScorePatch struct {
	IsHidden   *bool
	Categories []string
	Score      *float64
}

type TimeSeriesPoint

type TimeSeriesPoint struct {
	Name      string    `gorm:"primaryKey"`
	Timestamp time.Time `gorm:"primaryKey"`
	Value     float64
}

type Value

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

func Integer

func Integer(name string, value int) Value

func String

func String(name, value string) Value

func Time

func Time(name string, value time.Time) Value

Jump to

Keyboard shortcuts

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