From 469c27329274df602893c321cf923d875a69ccd7 Mon Sep 17 00:00:00 2001 From: serr Date: Mon, 6 Jan 2025 14:32:37 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20=D0=B4=D0=B0?= =?UTF-8?q?=D0=BC=D0=BF=D1=8B=20=D0=BA=D1=8D=D1=88=D0=B0=20=D0=B8=20=D0=B2?= =?UTF-8?q?=D0=BE=D1=81=D1=81=D1=82=D0=B0=D0=BD=D0=B0=D0=B2=D0=BB=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0=D1=82=D1=8C=20=D0=B8=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- candycache.go | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/candycache.go b/candycache.go index d8d9db0..0c2aeb6 100644 --- a/candycache.go +++ b/candycache.go @@ -3,13 +3,20 @@ package candycache import ( "encoding/json" "errors" - "fmt" "io" "reflect" "sync" "time" ) +// JSON структура для создания/загрузки дампов + +type Dump struct { + Key string `json:"key"` + DestroyTimestamp int64 `json:"destroyTimestamp"` + Data interface{} `json:"data"` +} + // Структура виде ключ-значение для возвращения списка элементов кэша с их ключами. type KeyItemPair struct { Key string @@ -202,11 +209,7 @@ func (c *Cache) Save(w io.Writer) error { encoder := json.NewEncoder(w) first := true for key, item := range c.storage { - entry := struct { - Key string `json:"key"` - DestroyTimestamp int64 `json:"destroyTimestamp"` - Data interface{} `json:"data"` - }{ + entry := Dump{ Key: key, DestroyTimestamp: item.destroyTimestamp, Data: item.data, @@ -239,18 +242,11 @@ func (c *Cache) Load(r io.Reader) error { decoder := json.NewDecoder(r) if _, err := decoder.Token(); err != nil { - if err == io.EOF { - return fmt.Errorf("файл пустой или имеет неправильный формат") - } return err } for decoder.More() { - var entry struct { - Key string `json:"key"` - DestroyTimestamp int64 `json:"destroyTimestamp"` - Data interface{} `json:"data"` - } + entry := Dump{} if err := decoder.Decode(&entry); err != nil { return err