mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Init commit - implement errors package on things service Signed-off-by: Ivan Milošević <iva@blokovi.com> * things service errors issue Signed-off-by: Ivan Milošević <iva@blokovi.com> * Decode errors Add authn service to run script Signed-off-by: Ivan Milošević <iva@blokovi.com> * Modify tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * service_test Signed-off-by: Ivan Milošević <iva@blokovi.com> * debug lines Signed-off-by: Ivan Milošević <iva@blokovi.com> * Regulate tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * Improve errors in Redis Signed-off-by: Ivan Milošević <iva@blokovi.com> * Remove dead code Inline if conditions Rename err var Signed-off-by: Ivan Milošević <iva@blokovi.com> * Transform errors messages to lowercase Signed-off-by: Ivan Milošević <iva@blokovi.com> * improve errors package Signed-off-by: Ivan Milošević <iva@blokovi.com> * modify wrap method inline wrapping errors in redis Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add copyright to errors package Signed-off-by: Ivan Milošević <iva@blokovi.com> * wrapping nil error returns wrapper (instead of nil) Signed-off-by: Ivan Milošević <iva@blokovi.com> * move response messages in test to vars Signed-off-by: Ivan Milošević <iva@blokovi.com> * golangcibot review fix Signed-off-by: Ivan Milošević <iva@blokovi.com> * golangbot fix review in transport Signed-off-by: Ivan Milošević <iva@blokovi.com>
78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package redis
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/go-redis/redis"
|
|
"github.com/mainflux/mainflux/errors"
|
|
"github.com/mainflux/mainflux/things"
|
|
)
|
|
|
|
const (
|
|
keyPrefix = "thing_key"
|
|
idPrefix = "thing"
|
|
)
|
|
|
|
// ErrRedisThingSave indicates error while saving Thing in redis cache
|
|
var ErrRedisThingSave = errors.New("saving thing in redis cache error")
|
|
|
|
// ErrRedisThingID indicates error while geting Thing ID from redis cache
|
|
var ErrRedisThingID = errors.New("get thing id from redis cache error")
|
|
|
|
// ErrRedisThingRemove indicates error while removing Thing from redis cache
|
|
var ErrRedisThingRemove = errors.New("remove thing from redis cache error")
|
|
|
|
var _ things.ThingCache = (*thingCache)(nil)
|
|
|
|
type thingCache struct {
|
|
client *redis.Client
|
|
}
|
|
|
|
// NewThingCache returns redis thing cache implementation.
|
|
func NewThingCache(client *redis.Client) things.ThingCache {
|
|
return &thingCache{
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
func (tc *thingCache) Save(_ context.Context, thingKey string, thingID string) error {
|
|
tkey := fmt.Sprintf("%s:%s", keyPrefix, thingKey)
|
|
if err := tc.client.Set(tkey, thingID, 0).Err(); err != nil {
|
|
return errors.Wrap(ErrRedisThingSave, err)
|
|
}
|
|
|
|
tid := fmt.Sprintf("%s:%s", idPrefix, thingID)
|
|
if err := tc.client.Set(tid, thingKey, 0).Err(); err != nil {
|
|
return errors.Wrap(ErrRedisThingSave, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (tc *thingCache) ID(_ context.Context, thingKey string) (string, error) {
|
|
tkey := fmt.Sprintf("%s:%s", keyPrefix, thingKey)
|
|
thingID, err := tc.client.Get(tkey).Result()
|
|
if err != nil {
|
|
return "", errors.Wrap(ErrRedisThingID, err)
|
|
}
|
|
|
|
return thingID, nil
|
|
}
|
|
|
|
func (tc *thingCache) Remove(_ context.Context, thingID string) error {
|
|
tid := fmt.Sprintf("%s:%s", idPrefix, thingID)
|
|
key, err := tc.client.Get(tid).Result()
|
|
if err != nil {
|
|
return errors.Wrap(ErrRedisThingRemove, err)
|
|
}
|
|
|
|
tkey := fmt.Sprintf("%s:%s", keyPrefix, key)
|
|
if err := tc.client.Del(tkey, tid).Err(); err != nil {
|
|
return errors.Wrap(ErrRedisThingRemove, err)
|
|
}
|
|
return nil
|
|
}
|