-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
355cef5
commit a6be456
Showing
7 changed files
with
112 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package cache | ||
|
||
import ( | ||
"EverythingSuckz/fsb/types" | ||
"bytes" | ||
"encoding/gob" | ||
"sync" | ||
|
||
"github.com/coocood/freecache" | ||
"github.com/gotd/td/tg" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var cache *Cache | ||
|
||
type Cache struct { | ||
cache *freecache.Cache | ||
mu sync.RWMutex | ||
log *zap.Logger | ||
} | ||
|
||
func InitCache(log *zap.Logger) { | ||
log = log.Named("cache") | ||
gob.Register(types.File{}) | ||
gob.Register(tg.InputDocumentFileLocation{}) | ||
defer log.Sugar().Info("Initialized") | ||
cache = &Cache{cache: freecache.NewCache(10 * 1024 * 1024), log: log} | ||
} | ||
|
||
func GetCache() *Cache { | ||
return cache | ||
} | ||
|
||
func (c *Cache) Get(key string, value *types.File) error { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
data, err := cache.cache.Get([]byte(key)) | ||
if err != nil { | ||
return err | ||
} | ||
dec := gob.NewDecoder(bytes.NewReader(data)) | ||
err = dec.Decode(&value) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Cache) Set(key string, value *types.File, expireSeconds int) error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
var buf bytes.Buffer | ||
enc := gob.NewEncoder(&buf) | ||
err := enc.Encode(value) | ||
if err != nil { | ||
return err | ||
} | ||
cache.cache.Set([]byte(key), buf.Bytes(), expireSeconds) | ||
return nil | ||
} | ||
|
||
func (c *Cache) Delete(key string) error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
cache.cache.Del([]byte(key)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters