1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00
Dušan Borovčanin 516c02bebe
MF-1378 - Update dependencies (#1379)
* Update dependencies

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix compose files and configs

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Upgrade image versions

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Update Postgres version

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Update test dependencies

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Fix fkey error handling

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
2021-05-20 20:53:56 +02:00

62 lines
1.5 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package redis
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/mainflux/mainflux/pkg/errors"
"github.com/mainflux/mainflux/things"
)
const chanPrefix = "channel"
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(ctx context.Context, chanID, thingID string) error {
cid, tid := kv(chanID, thingID)
if err := cc.client.SAdd(ctx, cid, tid).Err(); err != nil {
return errors.Wrap(things.ErrConnect, err)
}
return nil
}
func (cc channelCache) HasThing(ctx context.Context, chanID, thingID string) bool {
cid, tid := kv(chanID, thingID)
return cc.client.SIsMember(ctx, cid, tid).Val()
}
func (cc channelCache) Disconnect(ctx context.Context, chanID, thingID string) error {
cid, tid := kv(chanID, thingID)
if err := cc.client.SRem(ctx, cid, tid).Err(); err != nil {
return errors.Wrap(things.ErrDisconnect, err)
}
return nil
}
func (cc channelCache) Remove(ctx context.Context, chanID string) error {
cid, _ := kv(chanID, "0")
if err := cc.client.Del(ctx, cid).Err(); err != nil {
return errors.Wrap(things.ErrRemoveEntity, err)
}
return nil
}
// Generates key-value pair
func kv(chanID, thingID string) (string, string) {
cid := fmt.Sprintf("%s:%s", chanPrefix, chanID)
return cid, thingID
}