mirror of
https://github.com/derfenix/webarchive.git
synced 2026-03-11 22:40:58 +03:00
Fix reduce network calls count for the target url
This commit is contained in:
42
entity/cache.go
Normal file
42
entity/cache.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func NewCache() *Cache {
|
||||
return &Cache{data: make([]byte, 0, 1024*512)}
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
mu sync.RWMutex
|
||||
data []byte
|
||||
}
|
||||
|
||||
func (c *Cache) Write(p []byte) (n int, err error) {
|
||||
c.mu.Lock()
|
||||
c.data = append(c.data, p...)
|
||||
c.mu.Unlock()
|
||||
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (c *Cache) Get() []byte {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
return c.data
|
||||
}
|
||||
|
||||
func (c *Cache) Reader() io.Reader {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
if len(c.data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return bytes.NewBuffer(c.data)
|
||||
}
|
||||
Reference in New Issue
Block a user