localcache

package
Version: v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2022 License: MIT Imports: 5 Imported by: 4

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/clouddatastore"
	"go.mercari.io/datastore/dsmiddleware/localcache"
	"go.mercari.io/datastore/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 added in v0.15.0

type CacheOption interface {
	Apply(*cacheHandler)
}

A CacheOption is an option for cache.

func WithExcludeKinds added in v0.15.0

func WithExcludeKinds(kinds ...string) CacheOption

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

func WithExpireDuration added in v0.15.0

func WithExpireDuration(d time.Duration) CacheOption

WithExpireDuration creates a ClientOption to expire at a specified time.

func WithIncludeKinds added in v0.15.0

func WithIncludeKinds(kinds ...string) CacheOption

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

func WithKeyFilter added in v0.15.0

func WithKeyFilter(f storagecache.KeyFilter) CacheOption

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

func WithLogger added in v0.15.0

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