Skip to content

Commit

Permalink
Not exposing getOrCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSandison committed Jun 13, 2024
1 parent ca48813 commit 6bc1d7e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/gocache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (c *Cache[K, V]) HasKey(k K) (bool, error) {
}

func (c *Cache[K, V]) Get(k K, getter func() (*V, error)) (*V, error) {
valpromise, alreadyExists, _ := c.cache.GetOrCreate(k, NewPromise[V]())
valpromise, alreadyExists, _ := c.cache.getOrCreate(k, NewPromise[V]())
if alreadyExists {
val, err := valpromise.Wait()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gocache/partitioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *partitionedCache[K, V]) putUnsafe(key K, value *V) {
c.lru[partition].push(e)
}

func (c *partitionedCache[K, V]) GetOrCreate(key K, value *V) (*V, bool, *Promise[bool]) {
func (c *partitionedCache[K, V]) getOrCreate(key K, value *V) (*V, bool, *Promise[bool]) {
p := NewPromise[bool]()
// first, try to read the value by acquiring the read lock only
c.rUpgradeableLock(key)
Expand Down
12 changes: 6 additions & 6 deletions pkg/gocache/partitioned_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ func TestPartitionedCache(t *testing.T) {
num := 6

cache := newPartitionedCached[string, int](sizeOfPartition, numPartitions, 1*time.Hour)
_, exists, p := cache.GetOrCreate("A", &num)
_, exists, p := cache.getOrCreate("A", &num)
assert.False(t, exists)

p.Wait()

exists = cache.HasKey("A")
assert.True(t, exists)

val, exists, _ := cache.GetOrCreate("A", &num)
val, exists, _ := cache.getOrCreate("A", &num)
assert.True(t, exists)
assert.Equal(t, 6, *val)
}
Expand All @@ -49,7 +49,7 @@ func TestPartitionedCacheEviction(t *testing.T) {
cache := newPartitionedCached[string, int](sizeOfPartition, numPartitions, time.Hour)
for i := 0; i < 100000; i++ {
key := randString(16)
_, existsAlready, p := cache.GetOrCreate(key, nil)
_, existsAlready, p := cache.getOrCreate(key, nil)
assert.False(t, existsAlready)
p.Wait()
assert.LessOrEqual(t, cache.PartitionLen(key), sizeOfPartition)
Expand All @@ -61,13 +61,13 @@ func TestPartitionedCacheTTL(t *testing.T) {
numPartitions := 4
cache := newPartitionedCached[string, int](sizeOfPartition, numPartitions, time.Microsecond)

_, existsAlready, p := cache.GetOrCreate("A", nil)
_, existsAlready, p := cache.getOrCreate("A", nil)
assert.False(t, existsAlready)
p.Wait()

time.Sleep(time.Millisecond)

_, existsAlready, _ = cache.GetOrCreate("A", nil)
_, existsAlready, _ = cache.getOrCreate("A", nil)
assert.False(t, existsAlready)
}

Expand All @@ -85,7 +85,7 @@ func TestPartitionedCacheAsyncTest(t *testing.T) {
for j := 0; j < numActions; j++ {
v := j
k := randLetter()
_, _, p := cache.GetOrCreate(k, &v)
_, _, p := cache.getOrCreate(k, &v)
p.Wait()
assert.GreaterOrEqual(t, cache.PartitionLen(k), 1)
assert.LessOrEqual(t, cache.PartitionLen(k), sizeOfPartition)
Expand Down

0 comments on commit 6bc1d7e

Please sign in to comment.