aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/cloud.google.com/go/internal/atomiccache
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-06-13 19:31:19 +0200
committerGitHub <noreply@github.com>2017-06-13 19:31:19 +0200
commit5b060131006494cbc077f08b9b2fbf172f3eb239 (patch)
tree04f8586899db96f7fd8e7bc6a010fc10f1e2bb3b /vendor/cloud.google.com/go/internal/atomiccache
parentcd8e13f826ff24f5f8e0b8de1b9d3373aaf93d2f (diff)
parent612b82714b3e6660bf702f801ab96aacb3432e1f (diff)
Merge pull request #226 from google/dvyukov-vendor
vendor: vendor dependencies
Diffstat (limited to 'vendor/cloud.google.com/go/internal/atomiccache')
-rw-r--r--vendor/cloud.google.com/go/internal/atomiccache/atomiccache.go58
-rw-r--r--vendor/cloud.google.com/go/internal/atomiccache/atomiccache_test.go46
2 files changed, 104 insertions, 0 deletions
diff --git a/vendor/cloud.google.com/go/internal/atomiccache/atomiccache.go b/vendor/cloud.google.com/go/internal/atomiccache/atomiccache.go
new file mode 100644
index 000000000..2bea8a150
--- /dev/null
+++ b/vendor/cloud.google.com/go/internal/atomiccache/atomiccache.go
@@ -0,0 +1,58 @@
+// Copyright 2016 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package atomiccache provides a map-based cache that supports very fast
+// reads.
+package atomiccache
+
+import (
+ "sync"
+ "sync/atomic"
+)
+
+type mapType map[interface{}]interface{}
+
+// Cache is a map-based cache that supports fast reads via use of atomics.
+// Writes are slow, requiring a copy of the entire cache.
+// The zero Cache is an empty cache, ready for use.
+type Cache struct {
+ val atomic.Value // mapType
+ mu sync.Mutex // used only by writers
+}
+
+// Get returns the value of the cache at key. If there is no value,
+// getter is called to provide one, and the cache is updated.
+// The getter function may be called concurrently. It should be pure,
+// returning the same value for every call.
+func (c *Cache) Get(key interface{}, getter func() interface{}) interface{} {
+ mp, _ := c.val.Load().(mapType)
+ if v, ok := mp[key]; ok {
+ return v
+ }
+
+ // Compute value without lock.
+ // Might duplicate effort but won't hold other computations back.
+ newV := getter()
+
+ c.mu.Lock()
+ mp, _ = c.val.Load().(mapType)
+ newM := make(mapType, len(mp)+1)
+ for k, v := range mp {
+ newM[k] = v
+ }
+ newM[key] = newV
+ c.val.Store(newM)
+ c.mu.Unlock()
+ return newV
+}
diff --git a/vendor/cloud.google.com/go/internal/atomiccache/atomiccache_test.go b/vendor/cloud.google.com/go/internal/atomiccache/atomiccache_test.go
new file mode 100644
index 000000000..33105b343
--- /dev/null
+++ b/vendor/cloud.google.com/go/internal/atomiccache/atomiccache_test.go
@@ -0,0 +1,46 @@
+// Copyright 2016 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package atomiccache
+
+import (
+ "fmt"
+ "testing"
+)
+
+func TestGet(t *testing.T) {
+ var c Cache
+ called := false
+ get := func(k interface{}) interface{} {
+ return c.Get(k, func() interface{} {
+ called = true
+ return fmt.Sprintf("v%d", k)
+ })
+ }
+ got := get(1)
+ if want := "v1"; got != want {
+ t.Errorf("got %v, want %v", got, want)
+ }
+ if !called {
+ t.Error("getter not called, expected a call")
+ }
+ called = false
+ got = get(1)
+ if want := "v1"; got != want {
+ t.Errorf("got %v, want %v", got, want)
+ }
+ if called {
+ t.Error("getter unexpectedly called")
+ }
+}