diff options
author | Valery Piashchynski <[email protected]> | 2021-01-05 19:46:10 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-01-05 19:46:10 +0300 |
commit | 49e2440dd0be8b54ad08586e822333a3ed5e0db6 (patch) | |
tree | 786d73608f52a8f5255951d8f5b06e5f6a00f6fd /plugins | |
parent | 763de0b501e3d427c1751e8972bda2be415ed224 (diff) |
Fix LGTM warnings, reduce memory allocations when looping over maps
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/kv/memcached/plugin.go | 34 | ||||
-rw-r--r-- | plugins/kv/memory/storage.go | 59 |
2 files changed, 46 insertions, 47 deletions
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 } |