diff options
-rw-r--r-- | .github/workflows/build.yml | 8 | ||||
-rw-r--r-- | plugins/kv/memcached/plugin.go | 34 | ||||
-rw-r--r-- | plugins/kv/memory/storage.go | 59 |
3 files changed, 50 insertions, 51 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ea35c8e5..f9efbde0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -37,12 +37,12 @@ jobs: uses: actions/checkout@v2 - name: Get Composer Cache Directory - if: ${{ matrix.os == 'ubuntu-20.04' || matrix.os == 'macos-latest' }} + if: ${{ matrix.os != 'windows-latest' }} id: composer-cache run: echo "::set-output name=dir::$(composer config cache-files-dir)" - name: Init Composer Cache # Docs: <https://git.io/JfAKn#php---composer> - if: ${{ matrix.os == 'ubuntu-20.04' || matrix.os == 'macos-latest' }} + if: ${{ matrix.os != 'windows-latest' }} uses: actions/cache@v2 with: path: ${{ steps.composer-cache.outputs.dir }} @@ -86,7 +86,7 @@ jobs: go test -v -race -cover -tags=debug ./tests/plugins/static - name: Run golang tests on Linux and MacOS - if: ${{ matrix.os == 'ubuntu-20.04' || matrix.os == 'macos-latest' }} + if: ${{ matrix.os != 'windows-latest' }} run: | mkdir ./coverage-ci go test -v -race -cover -tags=debug -coverprofile=./coverage-ci/utils.txt -covermode=atomic ./utils @@ -111,7 +111,7 @@ jobs: cat ./coverage-ci/*.txt > ./coverage-ci/summary.txt - uses: codecov/codecov-action@v1 # Docs: <https://github.com/codecov/codecov-action> - if: ${{ matrix.os == 'ubuntu-20.04' || matrix.os == 'macos-latest' }} + if: ${{ matrix.os != 'windows-latest' }} with: token: ${{ secrets.CODECOV_TOKEN }} file: ./coverage-ci/summary.txt diff --git a/plugins/kv/memcached/plugin.go b/plugins/kv/memcached/plugin.go index bd0a207d..69f96bfe 100644 --- a/plugins/kv/memcached/plugin.go +++ b/plugins/kv/memcached/plugin.go @@ -65,18 +65,18 @@ func (s Plugin) Has(ctx context.Context, keys ...string) (map[string]bool, error return nil, errors.E(op, errors.NoKeys) } m := make(map[string]bool, len(keys)) - for _, key := range keys { - keyTrimmed := strings.TrimSpace(key) + for i := range keys { + keyTrimmed := strings.TrimSpace(keys[i]) if keyTrimmed == "" { return nil, errors.E(op, errors.EmptyKey) } - exist, err := s.client.Get(key) + exist, err := s.client.Get(keys[i]) // ErrCacheMiss means that a Get failed because the item wasn't present. if err != nil && err != memcache.ErrCacheMiss { return nil, err } if exist != nil { - m[key] = true + m[keys[i]] = true } } return m, nil @@ -113,23 +113,23 @@ func (s Plugin) MGet(ctx context.Context, keys ...string) (map[string]interface{ } // should not be empty keys - for _, key := range keys { - keyTrimmed := strings.TrimSpace(key) + for i := range keys { + keyTrimmed := strings.TrimSpace(keys[i]) if keyTrimmed == "" { return nil, errors.E(op, errors.EmptyKey) } } m := make(map[string]interface{}, len(keys)) - for _, key := range keys { + for i := range keys { // Here also MultiGet - data, err := s.client.Get(key) + data, err := s.client.Get(keys[i]) // ErrCacheMiss means that a Get failed because the item wasn't present. if err != nil && err != memcache.ErrCacheMiss { return nil, err } if data != nil { - m[key] = data.Value + m[keys[i]] = data.Value } } @@ -184,13 +184,13 @@ func (s Plugin) Set(ctx context.Context, items ...kv.Item) error { // Zero means the Item has no expiration time. func (s Plugin) MExpire(ctx context.Context, items ...kv.Item) error { const op = errors.Op("memcached MExpire") - for _, item := range items { - if item.TTL == "" || strings.TrimSpace(item.Key) == "" { + for i := range items { + if items[i].TTL == "" || strings.TrimSpace(items[i].Key) == "" { return errors.E(op, errors.Str("should set timeout and at least one key")) } // verify provided TTL - t, err := time.Parse(time.RFC3339, item.TTL) + t, err := time.Parse(time.RFC3339, items[i].TTL) if err != nil { return err } @@ -200,7 +200,7 @@ func (s Plugin) MExpire(ctx context.Context, items ...kv.Item) error { // into the future at which time the item will expire. Zero means the item has // no expiration time. ErrCacheMiss is returned if the key is not in the cache. // The key must be at most 250 bytes in length. - err = s.client.Touch(item.Key, int32(t.Unix())) + err = s.client.Touch(items[i].Key, int32(t.Unix())) if err != nil { return err } @@ -221,15 +221,15 @@ func (s Plugin) Delete(ctx context.Context, keys ...string) error { } // should not be empty keys - for _, key := range keys { - keyTrimmed := strings.TrimSpace(key) + for i := range keys { + keyTrimmed := strings.TrimSpace(keys[i]) if keyTrimmed == "" { return errors.E(op, errors.EmptyKey) } } - for _, key := range keys { - err := s.client.Delete(key) + for i := range keys { + err := s.client.Delete(keys[i]) // ErrCacheMiss means that a Get failed because the item wasn't present. if err != nil && err != memcache.ErrCacheMiss { return err diff --git a/plugins/kv/memory/storage.go b/plugins/kv/memory/storage.go index 1b6cb580..f4bdacea 100644 --- a/plugins/kv/memory/storage.go +++ b/plugins/kv/memory/storage.go @@ -72,14 +72,14 @@ func (s Plugin) Has(ctx context.Context, keys ...string) (map[string]bool, error return nil, errors.E(op, errors.NoKeys) } m := make(map[string]bool) - for _, key := range keys { - keyTrimmed := strings.TrimSpace(key) + for i := range keys { + keyTrimmed := strings.TrimSpace(keys[i]) if keyTrimmed == "" { return nil, errors.E(op, errors.EmptyKey) } - if _, ok := s.heap.Load(key); ok { - m[key] = true + if _, ok := s.heap.Load(keys[i]); ok { + m[keys[i]] = true } } @@ -109,8 +109,8 @@ func (s Plugin) MGet(ctx context.Context, keys ...string) (map[string]interface{ } // should not be empty keys - for _, key := range keys { - keyTrimmed := strings.TrimSpace(key) + for i := range keys { + keyTrimmed := strings.TrimSpace(keys[i]) if keyTrimmed == "" { return nil, errors.E(op, errors.EmptyKey) } @@ -118,9 +118,9 @@ func (s Plugin) MGet(ctx context.Context, keys ...string) (map[string]interface{ m := make(map[string]interface{}, len(keys)) - for _, key := range keys { - if value, ok := s.heap.Load(key); ok { - m[key] = value + for i := range keys { + if value, ok := s.heap.Load(keys[i]); ok { + m[keys[i]] = value } } @@ -133,17 +133,17 @@ func (s Plugin) Set(ctx context.Context, items ...kv.Item) error { return errors.E(op, errors.NoKeys) } - for _, item := range items { + for i := range items { // TTL is set - if item.TTL != "" { + if items[i].TTL != "" { // check the TTL in the item - _, err := time.Parse(time.RFC3339, item.TTL) + _, err := time.Parse(time.RFC3339, items[i].TTL) if err != nil { return err } } - s.heap.Store(item.Key, item) + s.heap.Store(items[i].Key, items[i]) } return nil } @@ -152,15 +152,15 @@ func (s Plugin) Set(ctx context.Context, items ...kv.Item) error { // If key already has the expiration time, it will be overwritten func (s Plugin) MExpire(ctx context.Context, items ...kv.Item) error { const op = errors.Op("in-memory storage MExpire") - for _, item := range items { - if item.TTL == "" || strings.TrimSpace(item.Key) == "" { + for i := range items { + if items[i].TTL == "" || strings.TrimSpace(items[i].Key) == "" { return errors.E(op, errors.Str("should set timeout and at least one key")) } // if key exist, overwrite it value - if pItem, ok := s.heap.Load(item.Key); ok { + if pItem, ok := s.heap.Load(items[i].Key); ok { // check that time is correct - _, err := time.Parse(time.RFC3339, item.TTL) + _, err := time.Parse(time.RFC3339, items[i].TTL) if err != nil { return errors.E(op, err) } @@ -168,10 +168,10 @@ func (s Plugin) MExpire(ctx context.Context, items ...kv.Item) error { // guess that t is in the future // in memory is just FOR TESTING PURPOSES // LOGIC ISN'T IDEAL - s.heap.Store(item.Key, kv.Item{ - Key: item.Key, + s.heap.Store(items[i].Key, kv.Item{ + Key: items[i].Key, Value: tmp.Value, - TTL: item.TTL, + TTL: items[i].TTL, }) } } @@ -186,8 +186,8 @@ func (s Plugin) TTL(ctx context.Context, keys ...string) (map[string]interface{} } // should not be empty keys - for _, key := range keys { - keyTrimmed := strings.TrimSpace(key) + for i := range keys { + keyTrimmed := strings.TrimSpace(keys[i]) if keyTrimmed == "" { return nil, errors.E(op, errors.EmptyKey) } @@ -195,9 +195,9 @@ func (s Plugin) TTL(ctx context.Context, keys ...string) (map[string]interface{} m := make(map[string]interface{}, len(keys)) - for _, key := range keys { - if item, ok := s.heap.Load(key); ok { - m[key] = item.(kv.Item).TTL + for i := range keys { + if item, ok := s.heap.Load(keys[i]); ok { + m[keys[i]] = item.(kv.Item).TTL } } return m, nil @@ -210,22 +210,21 @@ func (s Plugin) Delete(ctx context.Context, keys ...string) error { } // should not be empty keys - for _, key := range keys { - keyTrimmed := strings.TrimSpace(key) + for i := range keys { + keyTrimmed := strings.TrimSpace(keys[i]) if keyTrimmed == "" { return errors.E(op, errors.EmptyKey) } } - for _, key := range keys { - s.heap.Delete(key) + for i := range keys { + s.heap.Delete(keys[i]) } return nil } // Close clears the in-memory storage func (s Plugin) Close() error { - s.heap = &sync.Map{} s.stop <- struct{}{} return nil } |