1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Aleksandar Novaković 0c77d84176 MF-382 - Improve performance by adding Redis cache for message auth (#383)
* Add redis caching for thing and channel connections

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Fix authorization caching flow

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update things documentation

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-09-04 22:19:43 +02:00

73 lines
1.4 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package redis
import (
"fmt"
"strconv"
"github.com/go-redis/redis"
"github.com/mainflux/mainflux/things"
)
const (
keyPrefix = "thing_key"
idPrefix = "thing"
)
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(thingKey string, thingID uint64) error {
tkey := fmt.Sprintf("%s:%s", keyPrefix, thingKey)
id := strconv.FormatUint(thingID, 10)
if err := tc.client.Set(tkey, id, 0).Err(); err != nil {
return err
}
tid := fmt.Sprintf("%s:%s", idPrefix, id)
return tc.client.Set(tid, thingKey, 0).Err()
}
func (tc *thingCache) ID(thingKey string) (uint64, error) {
tkey := fmt.Sprintf("%s:%s", keyPrefix, thingKey)
thingID, err := tc.client.Get(tkey).Result()
if err != nil {
return 0, err
}
id, err := strconv.ParseUint(thingID, 10, 64)
if err != nil {
return 0, err
}
return id, nil
}
func (tc *thingCache) Remove(thingID uint64) error {
tid := fmt.Sprintf("%s:%d", idPrefix, thingID)
key, err := tc.client.Get(tid).Result()
if err != nil {
return err
}
tkey := fmt.Sprintf("%s:%s", keyPrefix, key)
return tc.client.Del(tkey, tid).Err()
}