forked from mtusi2026/mtusi2026lab3
Перенес необходимые изменения из лабораторной работы №2
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package inmemory
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Cache[K comparable, V any] interface {
|
||||
Name() string
|
||||
GetAll() map[K]V
|
||||
GetList() []V
|
||||
Get(key K) (value V, ok bool)
|
||||
Find(predicate func(K, V) bool) []V
|
||||
}
|
||||
|
||||
type CopyAndWriteCache[K comparable, V any] struct {
|
||||
name string
|
||||
data unsafe.Pointer
|
||||
}
|
||||
|
||||
func NewCopyAndWriteCache[K comparable, V any](
|
||||
name string,
|
||||
) *CopyAndWriteCache[K, V] {
|
||||
c := CopyAndWriteCache[K, V]{
|
||||
name: name,
|
||||
}
|
||||
|
||||
emptyMap := make(map[K]V)
|
||||
atomic.StorePointer(&c.data, unsafe.Pointer(&emptyMap))
|
||||
|
||||
slog.Info("cache initialized", "name", name)
|
||||
|
||||
return &c
|
||||
}
|
||||
|
||||
func (c *CopyAndWriteCache[K, V]) Name() string {
|
||||
return c.name
|
||||
}
|
||||
|
||||
func (c *CopyAndWriteCache[K, V]) GetAll() map[K]V {
|
||||
ptr := atomic.LoadPointer(&c.data)
|
||||
if ptr == nil {
|
||||
return make(map[K]V)
|
||||
}
|
||||
|
||||
m := *(*map[K]V)(ptr)
|
||||
|
||||
out := make(map[K]V, len(m))
|
||||
for k, v := range m {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (c *CopyAndWriteCache[K, V]) GetList() []V {
|
||||
records := c.GetAll()
|
||||
out := make([]V, 0, len(records))
|
||||
for _, record := range records {
|
||||
out = append(out, record)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (c *CopyAndWriteCache[K, V]) Get(key K) (value V, ok bool) {
|
||||
ptr := atomic.LoadPointer(&c.data)
|
||||
if ptr == nil {
|
||||
var zero V
|
||||
return zero, false
|
||||
}
|
||||
|
||||
m := *(*map[K]V)(ptr)
|
||||
v, ok := m[key]
|
||||
return v, ok
|
||||
}
|
||||
|
||||
func (c *CopyAndWriteCache[K, V]) Find(predicate func(K, V) bool) []V {
|
||||
ptr := atomic.LoadPointer(&c.data)
|
||||
if ptr == nil {
|
||||
return []V{}
|
||||
}
|
||||
|
||||
m := *(*map[K]V)(ptr)
|
||||
|
||||
var result []V
|
||||
for key, value := range m {
|
||||
if predicate(key, value) {
|
||||
result = append(result, value)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (c *CopyAndWriteCache[K, V]) Store(key K, value V) bool {
|
||||
ptr := atomic.LoadPointer(&c.data)
|
||||
if ptr == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
m := *(*map[K]V)(ptr)
|
||||
|
||||
m[key] = value
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package outside
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/cache"
|
||||
)
|
||||
|
||||
const (
|
||||
// chatRedisKey equals "chat:{chat_id}" path
|
||||
chatRedisKey = "chat:%s"
|
||||
)
|
||||
|
||||
type ChatCache struct {
|
||||
client *cache.Client
|
||||
}
|
||||
|
||||
func NewChatCache(client *cache.Client) *ChatCache {
|
||||
return &ChatCache{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
type chat struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []uuid.UUID `json:"member_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (r *ChatCache) Create(ctx context.Context, req dto.Chat) error {
|
||||
data, err := json.Marshal(chat{
|
||||
ID: req.ID,
|
||||
Title: req.Title,
|
||||
MemberIDs: req.MemberIDs,
|
||||
CreatedAt: req.CreatedAt,
|
||||
})
|
||||
if err != nil {
|
||||
return apperr.Wrap(err, apperr.CodeInternal, "failed to marshal chat cache")
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, cache.OperationTimeout)
|
||||
defer cancel()
|
||||
err = r.client.Set(ctx, fmt.Sprintf(chatRedisKey, req.ID), data, 0).Err()
|
||||
if err != nil {
|
||||
return apperr.WrapRedis(err, "failed to create chat cache")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ChatCache) Get(ctx context.Context, chatID uuid.UUID) (*dto.Chat, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, cache.OperationTimeout)
|
||||
defer cancel()
|
||||
data, err := r.client.Get(ctx, fmt.Sprintf(chatRedisKey, chatID.String())).Bytes()
|
||||
if err != nil {
|
||||
return nil, apperr.WrapRedis(err, "failed to read chat cache")
|
||||
}
|
||||
|
||||
var item chat
|
||||
if err = json.Unmarshal(data, &item); err != nil {
|
||||
return nil, apperr.Wrap(err, apperr.CodeInternal, "failed to unmarshal chat cache")
|
||||
}
|
||||
|
||||
return &dto.Chat{
|
||||
ID: item.ID,
|
||||
Title: item.Title,
|
||||
MemberIDs: item.MemberIDs,
|
||||
CreatedAt: item.CreatedAt,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user