cache

package
v0.5.0-alpha Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: Apache-2.0 Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ItemNeighbors is sorted set of neighbors for each item.
	//  Global item neighbors      - item_neighbors/{item_id}
	//  Categorized item neighbors - item_neighbors/{item_id}/{category}
	ItemNeighbors = "item_neighbors"

	// ItemNeighborsDigest is digest of item neighbors configuration
	//	Item neighbors digest      - item_neighbors_digest/{item_id}
	ItemNeighborsDigest = "item_neighbors_digest"

	// UserNeighbors is sorted set of neighbors for each user.
	//  User neighbors      - user_neighbors/{user_id}
	UserNeighbors = "user_neighbors"

	// UserNeighborsDigest is digest of user neighbors configuration
	//  User neighbors digest      - user_neighbors_digest/{user_id}
	UserNeighborsDigest = "user_neighbors_digest"

	// CollaborativeRecommend is sorted set of collaborative filtering recommendations for each user.
	//  Global recommendation      - collaborative_recommend/{user_id}
	//  Categorized recommendation - collaborative_recommend/{user_id}/{category}
	CollaborativeRecommend = "collaborative_recommend" // collaborative filtering recommendation for each user

	// OfflineRecommend is sorted set of offline recommendation for each user.
	//  Global recommendation      - offline_recommend/{user_id}
	//  Categorized recommendation - offline_recommend/{user_id}/{category}
	OfflineRecommend = "offline_recommend"

	// OfflineRecommendDigest is digest of offline recommendation configuration.
	//	Recommendation digest      - offline_recommend_digest/{user_id}
	OfflineRecommendDigest = "offline_recommend_digest"

	// PopularItems is sorted set of popular items. The format of key:
	//  Global popular items      - latest_items
	//  Categorized popular items - latest_items/{category}
	PopularItems = "popular_items"

	// LatestItems is sorted set of the latest items. The format of key:
	//  Global latest items      - latest_items
	//  Categorized the latest items - latest_items/{category}
	LatestItems = "latest_items"

	// 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
	LastUpdateUserRecommendTime = "last_update_user_recommend_time" // the latest timestamp that a user's recommendation was updated
	LastUpdateUserNeighborsTime = "last_update_user_neighbors_time" // the latest timestamp that a user's neighbors item was updated
	LastUpdateItemNeighborsTime = "last_update_item_neighbors_time" // the latest timestamp that an item's neighbors was updated

	// GlobalMeta is global meta information
	GlobalMeta                 = "global_meta"
	DataImported               = "data_imported"
	NumUsers                   = "num_users"
	NumItems                   = "num_items"
	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
	UserNeighborIndexRecall    = "user_neighbor_index_recall"
	ItemNeighborIndexRecall    = "item_neighbor_index_recall"
	MatchingIndexRecall        = "matching_index_recall"
)

Variables

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

Functions

func ConvertDocumentsToValues

func ConvertDocumentsToValues(documents []Document) []string

func Key added in v0.3.1

func Key(keys ...string) string

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

func SortDocuments

func SortDocuments(documents []Document)

Types

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

	GetSet(ctx context.Context, key string) ([]string, error)
	SetSet(ctx context.Context, key string, members ...string) error
	AddSet(ctx context.Context, key string, members ...string) error
	RemSet(ctx context.Context, key string, members ...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)

	AddDocuments(ctx context.Context, collection, subset string, documents []Document) error
	SearchDocuments(ctx context.Context, collection, subset string, query []string, begin, end int) ([]Document, error)
	DeleteDocuments(ctx context.Context, collection []string, condition DocumentCondition) error
	UpdateDocuments(ctx context.Context, collection []string, id string, patch DocumentPatch) error

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

Database is the common interface for cache store.

func Open

func Open(path, tablePrefix string) (Database, error)

Open a connection to a database.

type Document

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

type DocumentAggregator

type DocumentAggregator struct {
	Documents map[string]*Document
	Timestamp time.Time
}

DocumentAggregator is used to keep the compatibility with the old recommender system and will be removed in the future. In old recommender system, the recommendation is genereated per category. In the new recommender system, the recommendation is generated globally.

func NewDocumentAggregator

func NewDocumentAggregator(timestamp time.Time) *DocumentAggregator

func (*DocumentAggregator) Add

func (aggregator *DocumentAggregator) Add(category string, values []string, scores []float64)

func (*DocumentAggregator) ToSlice

func (aggregator *DocumentAggregator) ToSlice() []Document

type DocumentCondition

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

func (*DocumentCondition) Check

func (condition *DocumentCondition) Check() error

type DocumentPatch

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

type Message

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

type MongoDB added in v0.4.0

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

func (MongoDB) AddDocuments

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

func (MongoDB) AddSet added in v0.4.0

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

func (MongoDB) AddTimeSeriesPoints

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

func (MongoDB) Close added in v0.4.0

func (m MongoDB) Close() error

func (MongoDB) Delete added in v0.4.0

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

func (MongoDB) DeleteDocuments

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

func (MongoDB) Get added in v0.4.0

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

func (MongoDB) GetSet added in v0.4.0

func (m MongoDB) GetSet(ctx context.Context, name string) ([]string, error)

func (MongoDB) GetTimeSeriesPoints

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

func (MongoDB) Init added in v0.4.0

func (m MongoDB) Init() error

func (MongoDB) Ping added in v0.4.10

func (m MongoDB) Ping() error

func (MongoDB) Pop

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

func (MongoDB) Purge added in v0.4.7

func (m MongoDB) Purge() error

func (MongoDB) Push

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

func (MongoDB) RemSet added in v0.4.0

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

func (MongoDB) Remain

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

func (MongoDB) Scan added in v0.4.1

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

func (MongoDB) SearchDocuments

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

func (MongoDB) Set added in v0.4.0

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

func (MongoDB) SetSet added in v0.4.0

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

func (MongoDB) UpdateDocuments

func (m MongoDB) UpdateDocuments(ctx context.Context, collections []string, id string, patch DocumentPatch) error

type NoDatabase

type NoDatabase struct{}

NoDatabase means no database used for cache.

func (NoDatabase) AddDocuments

func (NoDatabase) AddDocuments(_ context.Context, _, _ string, _ []Document) error

func (NoDatabase) AddSet added in v0.3.1

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

AddSet method of NoDatabase returns ErrNoDatabase.

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 added in v0.3.1

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

Delete method of NoDatabase returns ErrNoDatabase.

func (NoDatabase) DeleteDocuments

func (NoDatabase) DeleteDocuments(_ context.Context, _ []string, _ DocumentCondition) error

func (NoDatabase) Get added in v0.4.0

Get method of NoDatabase returns ErrNoDatabase.

func (NoDatabase) GetSet added in v0.3.1

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

GetSet method of NoDatabase returns ErrNoDatabase.

func (NoDatabase) GetTimeSeriesPoints

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

func (NoDatabase) Init added in v0.4.0

func (NoDatabase) Init() error

Init method of NoDatabase returns ErrNoDatabase.

func (NoDatabase) Ping added in v0.4.10

func (NoDatabase) Ping() error

func (NoDatabase) Pop

func (NoDatabase) Purge added in v0.4.7

func (NoDatabase) Purge() error

func (NoDatabase) Push

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

func (NoDatabase) RemSet added in v0.3.1

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

RemSet method of NoDatabase returns ErrNoDatabase.

func (NoDatabase) Remain

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

func (NoDatabase) Scan added in v0.4.1

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

func (NoDatabase) SearchDocuments

func (NoDatabase) SearchDocuments(_ context.Context, _, _ string, _ []string, _, _ int) ([]Document, error)

func (NoDatabase) Set added in v0.4.0

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

func (NoDatabase) SetSet added in v0.3.1

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

SetSet method of NoDatabase returns ErrNoDatabase.

func (NoDatabase) UpdateDocuments

func (NoDatabase) UpdateDocuments(_ context.Context, _ []string, _ string, _ DocumentPatch) error

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 Redis

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

Redis cache storage.

func (*Redis) AddDocuments

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

func (*Redis) AddSet added in v0.3.1

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

AddSet adds members to a set in Redis.

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 added in v0.3.1

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

Delete object from Redis.

func (*Redis) DeleteDocuments

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

func (*Redis) Get added in v0.4.0

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

Get returns a value from Redis.

func (*Redis) GetSet added in v0.3.1

func (r *Redis) GetSet(ctx context.Context, key string) ([]string, error)

GetSet returns members of a set from Redis.

func (*Redis) GetTimeSeriesPoints

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

func (*Redis) Init added in v0.4.0

func (r *Redis) Init() error

Init nothing.

func (*Redis) Ping added in v0.4.10

func (r *Redis) Ping() error

func (*Redis) Pop

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

func (*Redis) Purge added in v0.4.7

func (r *Redis) Purge() error

func (*Redis) Push

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

func (*Redis) RemSet added in v0.3.1

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

RemSet removes members from a set in Redis.

func (*Redis) Remain

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

func (*Redis) Scan added in v0.4.1

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

func (*Redis) SearchDocuments

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

func (*Redis) Set added in v0.4.0

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

func (*Redis) SetSet added in v0.3.1

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

SetSet overrides a set with members in Redis.

func (*Redis) UpdateDocuments

func (r *Redis) UpdateDocuments(ctx context.Context, collections []string, id string, patch DocumentPatch) error

type ReturnValue added in v0.4.0

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

func (*ReturnValue) Integer added in v0.4.0

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

func (*ReturnValue) String added in v0.4.0

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

func (*ReturnValue) Time added in v0.4.0

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

type SQLDatabase added in v0.4.0

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

func (*SQLDatabase) AddDocuments

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

func (*SQLDatabase) AddSet added in v0.4.0

func (db *SQLDatabase) AddSet(ctx context.Context, key string, members ...string) error

func (*SQLDatabase) AddTimeSeriesPoints

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

func (*SQLDatabase) Close added in v0.4.0

func (db *SQLDatabase) Close() error

func (*SQLDatabase) Delete added in v0.4.0

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

func (*SQLDatabase) DeleteDocuments

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

func (*SQLDatabase) Get added in v0.4.0

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

func (*SQLDatabase) GetSet added in v0.4.0

func (db *SQLDatabase) GetSet(ctx context.Context, key string) ([]string, error)

func (*SQLDatabase) GetTimeSeriesPoints

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

func (*SQLDatabase) Init added in v0.4.0

func (db *SQLDatabase) Init() error

func (*SQLDatabase) Ping added in v0.4.10

func (db *SQLDatabase) Ping() error

func (*SQLDatabase) Pop

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

func (*SQLDatabase) Purge added in v0.4.7

func (db *SQLDatabase) Purge() error

func (*SQLDatabase) Push

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

func (*SQLDatabase) RemSet added in v0.4.0

func (db *SQLDatabase) RemSet(ctx context.Context, key string, members ...string) error

func (*SQLDatabase) Remain

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

func (*SQLDatabase) Scan added in v0.4.1

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

func (*SQLDatabase) SearchDocuments

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

func (*SQLDatabase) Set added in v0.4.0

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

func (*SQLDatabase) SetSet added in v0.4.0

func (db *SQLDatabase) SetSet(ctx context.Context, key string, members ...string) error

func (*SQLDatabase) UpdateDocuments

func (db *SQLDatabase) UpdateDocuments(ctx context.Context, collections []string, id string, patch DocumentPatch) 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 added in v0.4.0

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

type SQLSet added in v0.4.4

type SQLSet struct {
	Name   string `gorm:"type:varchar(256);primaryKey"`
	Member string `gorm:"type:varchar(256);primaryKey"`
}

type SQLSortedSet added in v0.4.4

type SQLSortedSet struct {
	Name   string  `gorm:"type:varchar(256);primaryKey;index:name"`
	Member string  `gorm:"type:varchar(256);primaryKey"`
	Score  float64 `gorm:"type:double precision;not null;index:name"`
}

type SQLValue added in v0.4.4

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

type TimeSeriesPoint

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

type Value added in v0.4.0

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

func Integer added in v0.4.0

func Integer(name string, value int) Value

func String added in v0.4.0

func String(name, value string) Value

func Time added in v0.4.0

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