github.com/cs3org/reva/v2@v2.27.7/pkg/store/options.go (about)

     1  // Copyright 2018-2023 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package store
    20  
    21  import (
    22  	"context"
    23  	"time"
    24  
    25  	"go-micro.dev/v4/store"
    26  )
    27  
    28  type typeContextKey struct{}
    29  
    30  // Store determines the implementation:
    31  //   - "memory", for a in-memory implementation, which is also the default if noone matches
    32  //   - "noop", for a noop store (it does nothing)
    33  //   - "etcd", for etcd
    34  //   - "nats-js" for nats-js, needs to have TTL configured at creation
    35  //   - "redis", for redis
    36  //   - "redis-sentinel", for redis-sentinel
    37  //   - "ocmem", custom in-memory implementation, with fixed size and optimized prefix
    38  //     and suffix search
    39  func Store(val string) store.Option {
    40  	return func(o *store.Options) {
    41  		if o.Context == nil {
    42  			o.Context = context.Background()
    43  		}
    44  
    45  		o.Context = context.WithValue(o.Context, typeContextKey{}, val)
    46  	}
    47  }
    48  
    49  type sizeContextKey struct{}
    50  
    51  // Size configures the maximum capacity of the cache for the "ocmem" implementation,
    52  // in number of items that the cache can hold per table.
    53  // You can use 5000 to make the cache hold up to 5000 elements.
    54  // The parameter only affects to the "ocmem" implementation, the rest will ignore it.
    55  // If an invalid value is used, the default of 512 will be used instead.
    56  func Size(val int) store.Option {
    57  	return func(o *store.Options) {
    58  		if o.Context == nil {
    59  			o.Context = context.Background()
    60  		}
    61  
    62  		o.Context = context.WithValue(o.Context, sizeContextKey{}, val)
    63  	}
    64  }
    65  
    66  type ttlContextKey struct{}
    67  
    68  // TTL is the time to live for documents stored in the store
    69  func TTL(val time.Duration) store.Option {
    70  	return func(o *store.Options) {
    71  		if o.Context == nil {
    72  			o.Context = context.Background()
    73  		}
    74  
    75  		o.Context = context.WithValue(o.Context, ttlContextKey{}, val)
    76  	}
    77  }
    78  
    79  type disablePersistanceContextKey struct{}
    80  
    81  // DisablePersistence disables the persistence of the store by instructing it to use memory only.
    82  // Only supported by the `natsjs` and `natsjskv` implementations.
    83  func DisablePersistence(val bool) store.Option {
    84  	return func(o *store.Options) {
    85  		if o.Context == nil {
    86  			o.Context = context.Background()
    87  		}
    88  
    89  		o.Context = context.WithValue(o.Context, disablePersistanceContextKey{}, val)
    90  	}
    91  }
    92  
    93  type authenticationContextKey struct{}
    94  
    95  // Authentication configures the username and password to use for authentication.
    96  // Only supported by the `natsjskv` implementation.
    97  func Authentication(username, password string) store.Option {
    98  	return func(o *store.Options) {
    99  		if o.Context == nil {
   100  			o.Context = context.Background()
   101  		}
   102  
   103  		o.Context = context.WithValue(o.Context, authenticationContextKey{}, []string{username, password})
   104  	}
   105  }