go.mercari.io/datastore@v1.8.2/dsmiddleware/aememcache/option.go (about)

     1  package aememcache
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"go.mercari.io/datastore"
     8  	"go.mercari.io/datastore/dsmiddleware/storagecache"
     9  )
    10  
    11  // WithIncludeKinds creates a ClientOption that selects the Kind specified as the cache target.
    12  func WithIncludeKinds(kinds ...string) CacheOption {
    13  	return &withIncludeKinds{kinds}
    14  }
    15  
    16  type withIncludeKinds struct{ kinds []string }
    17  
    18  func (w *withIncludeKinds) Apply(o *cacheHandler) {
    19  	o.stOpts.Filters = append(o.stOpts.Filters, func(ctx context.Context, key datastore.Key) bool {
    20  		for _, incKind := range w.kinds {
    21  			if key.Kind() == incKind {
    22  				return true
    23  			}
    24  		}
    25  
    26  		return false
    27  	})
    28  }
    29  
    30  // WithExcludeKinds creates a ClientOption that selects the Kind unspecified as the cache target.
    31  func WithExcludeKinds(kinds ...string) CacheOption {
    32  	return &withExcludeKinds{kinds}
    33  }
    34  
    35  type withExcludeKinds struct{ kinds []string }
    36  
    37  func (w *withExcludeKinds) Apply(o *cacheHandler) {
    38  	o.stOpts.Filters = append(o.stOpts.Filters, func(ctx context.Context, key datastore.Key) bool {
    39  		for _, excKind := range w.kinds {
    40  			if key.Kind() == excKind {
    41  				return false
    42  			}
    43  		}
    44  
    45  		return true
    46  	})
    47  }
    48  
    49  // WithKeyFilter creates a ClientOption that selects the Keys specified as the cache target.
    50  func WithKeyFilter(f storagecache.KeyFilter) CacheOption {
    51  	return &withKeyFilter{f}
    52  }
    53  
    54  type withKeyFilter struct{ f storagecache.KeyFilter }
    55  
    56  func (w *withKeyFilter) Apply(o *cacheHandler) {
    57  	o.stOpts.Filters = append(o.stOpts.Filters, func(ctx context.Context, key datastore.Key) bool {
    58  		return w.f(ctx, key)
    59  	})
    60  }
    61  
    62  // WithLogger creates a ClientOption that uses the specified logger.
    63  func WithLogger(logf func(ctx context.Context, format string, args ...interface{})) CacheOption {
    64  	return &withLogger{logf}
    65  }
    66  
    67  type withLogger struct {
    68  	logf func(ctx context.Context, format string, args ...interface{})
    69  }
    70  
    71  func (w *withLogger) Apply(o *cacheHandler) {
    72  	o.logf = w.logf
    73  }
    74  
    75  // WithExpireDuration creates a ClientOption to expire at a specified time.
    76  func WithExpireDuration(d time.Duration) CacheOption {
    77  	return &withExpireDuration{d}
    78  }
    79  
    80  type withExpireDuration struct{ d time.Duration }
    81  
    82  func (w *withExpireDuration) Apply(o *cacheHandler) {
    83  	o.expireDuration = w.d
    84  }
    85  
    86  // WithCacheKey creates a ClientOption that specifies how to generate a cache key from datastore.Key.
    87  func WithCacheKey(f func(key datastore.Key) string) CacheOption {
    88  	return &withCacheKey{f}
    89  }
    90  
    91  type withCacheKey struct {
    92  	cacheKey func(key datastore.Key) string
    93  }
    94  
    95  func (w *withCacheKey) Apply(o *cacheHandler) {
    96  	o.cacheKey = w.cacheKey
    97  }