mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-01 13:48:56 +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>
71 lines
2.0 KiB
Go
71 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 chanPrefix = "channel"
|
|
|
|
// ErrRedisConnectChannel indicates error while adding connection in redis cache
|
|
var ErrRedisConnectChannel = errors.New("add connection to redis cache error")
|
|
|
|
// ErrRedisDisconnectChannel indicates error while removing connection from redis cache
|
|
var ErrRedisDisconnectChannel = errors.New("remove connection from redis cache error")
|
|
|
|
// ErrRedisRemoveChannel indicates error while removing channel from redis cache
|
|
var ErrRedisRemoveChannel = errors.New("remove channel from redis cache error")
|
|
|
|
var _ things.ChannelCache = (*channelCache)(nil)
|
|
|
|
type channelCache struct {
|
|
client *redis.Client
|
|
}
|
|
|
|
// NewChannelCache returns redis channel cache implementation.
|
|
func NewChannelCache(client *redis.Client) things.ChannelCache {
|
|
return channelCache{client: client}
|
|
}
|
|
|
|
func (cc channelCache) Connect(_ context.Context, chanID, thingID string) error {
|
|
cid, tid := kv(chanID, thingID)
|
|
if err := cc.client.SAdd(cid, tid).Err(); err != nil {
|
|
return errors.Wrap(ErrRedisConnectChannel, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (cc channelCache) HasThing(_ context.Context, chanID, thingID string) bool {
|
|
cid, tid := kv(chanID, thingID)
|
|
return cc.client.SIsMember(cid, tid).Val()
|
|
}
|
|
|
|
func (cc channelCache) Disconnect(_ context.Context, chanID, thingID string) error {
|
|
cid, tid := kv(chanID, thingID)
|
|
if err := cc.client.SRem(cid, tid).Err(); err != nil {
|
|
return errors.Wrap(ErrRedisDisconnectChannel, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (cc channelCache) Remove(_ context.Context, chanID string) error {
|
|
cid, _ := kv(chanID, "0")
|
|
if err := cc.client.Del(cid).Err(); err != nil {
|
|
return errors.Wrap(ErrRedisRemoveChannel, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Generates key-value pair
|
|
func kv(chanID, thingID string) (string, string) {
|
|
cid := fmt.Sprintf("%s:%s", chanPrefix, chanID)
|
|
return cid, thingID
|
|
}
|