2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-09-04 22:19:43 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2019-07-18 15:01:09 +02:00
|
|
|
"context"
|
2018-09-04 22:19:43 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/go-redis/redis"
|
2020-03-30 15:22:18 +02:00
|
|
|
"github.com/mainflux/mainflux/errors"
|
2018-09-04 22:19:43 +02:00
|
|
|
"github.com/mainflux/mainflux/things"
|
|
|
|
)
|
|
|
|
|
|
|
|
const chanPrefix = "channel"
|
|
|
|
|
2020-03-30 15:22:18 +02:00
|
|
|
// 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")
|
|
|
|
|
2018-09-04 22:19:43 +02:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (cc channelCache) Connect(_ context.Context, chanID, thingID string) error {
|
2018-09-04 22:19:43 +02:00
|
|
|
cid, tid := kv(chanID, thingID)
|
2020-03-30 15:22:18 +02:00
|
|
|
if err := cc.client.SAdd(cid, tid).Err(); err != nil {
|
|
|
|
return errors.Wrap(ErrRedisConnectChannel, err)
|
|
|
|
}
|
|
|
|
return nil
|
2018-09-04 22:19:43 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (cc channelCache) HasThing(_ context.Context, chanID, thingID string) bool {
|
2018-09-04 22:19:43 +02:00
|
|
|
cid, tid := kv(chanID, thingID)
|
|
|
|
return cc.client.SIsMember(cid, tid).Val()
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (cc channelCache) Disconnect(_ context.Context, chanID, thingID string) error {
|
2018-09-04 22:19:43 +02:00
|
|
|
cid, tid := kv(chanID, thingID)
|
2020-03-30 15:22:18 +02:00
|
|
|
if err := cc.client.SRem(cid, tid).Err(); err != nil {
|
|
|
|
return errors.Wrap(ErrRedisDisconnectChannel, err)
|
|
|
|
}
|
|
|
|
return nil
|
2018-09-04 22:19:43 +02:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:01:09 +02:00
|
|
|
func (cc channelCache) Remove(_ context.Context, chanID string) error {
|
2018-12-05 13:09:25 +01:00
|
|
|
cid, _ := kv(chanID, "0")
|
2020-03-30 15:22:18 +02:00
|
|
|
if err := cc.client.Del(cid).Err(); err != nil {
|
|
|
|
return errors.Wrap(ErrRedisRemoveChannel, err)
|
|
|
|
}
|
|
|
|
return nil
|
2018-09-04 22:19:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generates key-value pair
|
2018-12-05 13:09:25 +01:00
|
|
|
func kv(chanID, thingID string) (string, string) {
|
|
|
|
cid := fmt.Sprintf("%s:%s", chanPrefix, chanID)
|
|
|
|
return cid, thingID
|
2018-09-04 22:19:43 +02:00
|
|
|
}
|