mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-06 19:29:15 +08:00
59 lines
1012 B
Go
59 lines
1012 B
Go
![]() |
// Copyright (c) Mainflux
|
||
|
// SPDX-License-Identifier: Apache-2.0
|
||
|
|
||
|
package mocks
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"sync"
|
||
|
|
||
|
"github.com/mainflux/mainflux/pkg/errors"
|
||
|
"github.com/mainflux/mainflux/things/clients"
|
||
|
)
|
||
|
|
||
|
type clientCacheMock struct {
|
||
|
mu sync.Mutex
|
||
|
things map[string]string
|
||
|
}
|
||
|
|
||
|
// NewCache returns mock cache instance.
|
||
|
func NewCache() clients.Cache {
|
||
|
return &clientCacheMock{
|
||
|
things: make(map[string]string),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (tcm *clientCacheMock) Save(_ context.Context, key, id string) error {
|
||
|
tcm.mu.Lock()
|
||
|
defer tcm.mu.Unlock()
|
||
|
|
||
|
tcm.things[key] = id
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (tcm *clientCacheMock) ID(_ context.Context, key string) (string, error) {
|
||
|
tcm.mu.Lock()
|
||
|
defer tcm.mu.Unlock()
|
||
|
|
||
|
id, ok := tcm.things[key]
|
||
|
if !ok {
|
||
|
return "", errors.ErrNotFound
|
||
|
}
|
||
|
|
||
|
return id, nil
|
||
|
}
|
||
|
|
||
|
func (tcm *clientCacheMock) Remove(_ context.Context, id string) error {
|
||
|
tcm.mu.Lock()
|
||
|
defer tcm.mu.Unlock()
|
||
|
|
||
|
for key, val := range tcm.things {
|
||
|
if val == id {
|
||
|
delete(tcm.things, key)
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|