go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/authdb/cache.go (about)

     1  // Copyright 2016 The LUCI Authors.
     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  package authdb
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/common/data/caching/lazyslot"
    22  	"go.chromium.org/luci/common/data/rand/mathrand"
    23  )
    24  
    25  // DBCacheUpdater knows how to update local in-memory copy of DB.
    26  //
    27  // Used by NewDBCache.
    28  type DBCacheUpdater func(ctx context.Context, prev DB) (DB, error)
    29  
    30  // NewDBCache returns a provider of DB instances that uses local memory to
    31  // cache DB instances for 5-10 seconds. It uses supplied callback to refetch DB
    32  // from some permanent storage when cache expires.
    33  //
    34  // Even though the return value is technically a function, treat it as a heavy
    35  // stateful object, since it has the cache of DB in its closure.
    36  func NewDBCache(updater DBCacheUpdater) func(ctx context.Context) (DB, error) {
    37  	cacheSlot := lazyslot.Slot{}
    38  	return func(ctx context.Context) (DB, error) {
    39  		val, err := cacheSlot.Get(ctx, func(prev any) (db any, exp time.Duration, err error) {
    40  			prevDB, _ := prev.(DB)
    41  			if db, err = updater(ctx, prevDB); err == nil {
    42  				exp = 5*time.Second + time.Duration(mathrand.Get(ctx).Intn(5000))*time.Millisecond
    43  			}
    44  			return
    45  		})
    46  		if err != nil {
    47  			return nil, err
    48  		}
    49  		return val.(DB), nil
    50  	}
    51  }