localcache

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package localcache handles Put, Get etc to Datastore and provides caching by machine local memory. How the cache is used is explained in the storagecache package's document.

The local cache can not be deleted from other machines. Therefore, if the cache holding period becomes long, there is a possibility that the data is old. As a countermeasure, we recommend keeping the lifetime of the cache as long as processing one request.

Example (HowToUse)
package main

import (
	"context"

	"go.mercari.io/datastore/v2/clouddatastore"
	"go.mercari.io/datastore/v2/dsmiddleware/localcache"
	"go.mercari.io/datastore/v2/internal/testutils"
)

func main() {
	ctx := context.Background()
	client, err := clouddatastore.FromContext(ctx)
	if err != nil {
		panic(err)
	}
	defer client.Close()
	defer testutils.CleanUpAllEntities(ctx, client)

	mw := localcache.New()
	client.AppendMiddleware(mw)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheHandler

type CacheHandler interface {
	datastore.Middleware
	storagecache.Storage

	HasCache(key datastore.Key) bool
	DeleteCache(ctx context.Context, key datastore.Key)
	CacheKeys() []string
	CacheLen() int
	FlushLocalCache()
}

CacheHandler abstracts cache operations to Storage.

func New

func New(opts ...CacheOption) CacheHandler

New in-memory localcache middleware creates and returns.

type CacheOption

type CacheOption interface {
	Apply(*cacheHandler)
}

A CacheOption is an option for cache.

func WithExcludeKinds

func WithExcludeKinds(kinds ...string) CacheOption

WithExcludeKinds creates a ClientOption that selects the Kind unspecified as the cache target.

func WithExpireDuration

func WithExpireDuration(d time.Duration) CacheOption

WithExpireDuration creates a ClientOption to expire at a specified time.

func WithIncludeKinds

func WithIncludeKinds(kinds ...string) CacheOption

WithIncludeKinds creates a ClientOption that selects the Kind specified as the cache target.

func WithKeyFilter

func WithKeyFilter(f storagecache.KeyFilter) CacheOption

WithKeyFilter creates a ClientOption that selects the Keys specified as the cache target.

func WithLogger

func WithLogger(logf func(ctx context.Context, format string, args ...interface{})) CacheOption

WithLogger creates a ClientOption that uses the specified logger.

Jump to

Keyboard shortcuts

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