github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/shared/datastore_cached.go (about)

     1  // Copyright 2018 The WPT Dashboard Project. All rights reserved.
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package shared
     6  
     7  import (
     8  	"context"
     9  	"sync"
    10  	"time"
    11  )
    12  
    13  // testRunCacheTTL is the expiration for each test run in Redis.
    14  var testRunCacheTTL = 48 * time.Hour
    15  
    16  type cachedDatastore struct {
    17  	Datastore
    18  	ctx context.Context
    19  }
    20  
    21  func (d cachedDatastore) Get(k Key, dst interface{}) error {
    22  	if k.Kind() != "TestRun" {
    23  		return d.Datastore.Get(k, dst)
    24  	}
    25  
    26  	cs := NewObjectCachedStore(
    27  		d.ctx,
    28  		NewJSONObjectCache(d.ctx, NewRedisReadWritable(d.ctx, testRunCacheTTL)),
    29  		testRunObjectStore{d})
    30  	return cs.Get(getTestRunRedisKey(k.IntID()), k.IntID(), dst)
    31  }
    32  
    33  func (d cachedDatastore) GetMulti(keys []Key, dst interface{}) error {
    34  	for _, key := range keys {
    35  		if key.Kind() != "TestRun" {
    36  			return d.Datastore.GetMulti(keys, dst)
    37  		}
    38  	}
    39  
    40  	runs := dst.(TestRuns)
    41  	var wg sync.WaitGroup
    42  	var err error
    43  	for i := range keys {
    44  		wg.Add(1)
    45  		go func(i int) {
    46  			defer wg.Done()
    47  			if localErr := d.Get(keys[i], &runs[i]); localErr != nil {
    48  				err = localErr
    49  			}
    50  		}(i)
    51  	}
    52  	wg.Wait()
    53  	return err
    54  }
    55  
    56  // testRunObjectStore is an adapter from Datastore to ObjectStore.
    57  type testRunObjectStore struct {
    58  	cachedDatastore
    59  }
    60  
    61  func (d testRunObjectStore) Get(id, dst interface{}) error {
    62  	intID, ok := id.(int64)
    63  	if !ok {
    64  		return errDatastoreObjectStoreExpectedInt64
    65  	}
    66  	key := d.NewIDKey("TestRun", intID)
    67  	err := d.Datastore.Get(key, dst)
    68  	if err == nil {
    69  		run := dst.(*TestRun)
    70  		run.ID = key.IntID()
    71  	}
    72  	return d.Datastore.Get(key, dst)
    73  }