Implement caching previously queried (by chatter) city in weather module

This commit is contained in:
eleksir
2025-02-09 14:27:01 +03:00
parent a9e0095dc9
commit 07430d87ed
3 changed files with 59 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ package openweathermap
import (
"aleesa-webapp-go/internal/config"
"aleesa-webapp-go/internal/pcachedb"
"crypto/sha256"
"fmt"
"net/http"
"strconv"
@@ -357,4 +358,32 @@ func QueryOwmCache(cfg *config.MyConfig, item OwmItem) (string, error) {
return answer, err
}
// Записывает в кэш, кто каким городом интересовался в последний раз, когда спрашивал о погоде.
// STUB
func UpdateOwmUserCache(cfg *config.MyConfig, chatid string, entry string, value string) error {
var err error
// Chatid как есть лучше на диск не пробовать класть, мало ли что туда юзер впишет. Возьмём от него sha256.
data := []byte(chatid)
hash := fmt.Sprintf("%x", sha256.Sum256(data))
if err = pcachedb.SaveKeyWithValue(cfg, hash, entry, value); err != nil {
return fmt.Errorf("OWM-Cache: unable to save timestamp to cache: %w", err)
}
return err
}
// Возвращает из кэша город, которым интересовался пользователь из даденного чятика.
func QueryOwmUserCache(cfg *config.MyConfig, chatid string, entry string) string {
// Chatid как есть лучше на диск не пробовать класть, мало ли что туда юзер впишет. Возьмём от него sha256.
var (
data = []byte(chatid)
hash = fmt.Sprintf("%x", sha256.Sum256(data))
value = pcachedb.GetValue(cfg, hash, entry)
)
return value
}
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */