Caching policies added.

We used httpcache library to implement this. As it was not possible to support different policies, I mostly copied and modified it.
This commit is contained in:
Musab Gültekin
2019-07-07 12:18:40 +03:00
parent 0d6c2a6864
commit 90d2be2210
13 changed files with 2349 additions and 16 deletions

51
cache/leveldbcache/leveldbcache.go vendored Normal file
View File

@ -0,0 +1,51 @@
// Package leveldbcache provides an implementation of cache.Cache that
// uses github.com/syndtr/goleveldb/leveldb
package leveldbcache
import (
"github.com/syndtr/goleveldb/leveldb"
)
// Cache is an implementation of cache.Cache with leveldb storage
type Cache struct {
Db *leveldb.DB
}
// Get returns the response corresponding to key if present
func (c *Cache) Get(key string) (resp []byte, ok bool) {
var err error
resp, err = c.Db.Get([]byte(key), nil)
if err != nil {
return []byte{}, false
}
return resp, true
}
// Set saves a response to the cache as key
func (c *Cache) Set(key string, resp []byte) {
_ = c.Db.Put([]byte(key), resp, nil)
}
// Delete removes the response with key from the cache
func (c *Cache) Delete(key string) {
_ = c.Db.Delete([]byte(key), nil)
}
// New returns a new Cache that will store leveldb in path
func New(path string) (*Cache, error) {
cache := &Cache{}
var err error
cache.Db, err = leveldb.OpenFile(path, nil)
if err != nil {
return nil, err
}
return cache, nil
}
// NewWithDB returns a new Cache using the provided leveldb as underlying
// storage.
func NewWithDB(db *leveldb.DB) *Cache {
return &Cache{db}
}

24
cache/leveldbcache/leveldbcache_test.go vendored Normal file
View File

@ -0,0 +1,24 @@
package leveldbcache
import (
"github.com/geziyor/geziyor/cache"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestDiskCache(t *testing.T) {
tempDir, err := ioutil.TempDir("", "cache")
if err != nil {
t.Fatalf("TempDir: %v", err)
}
defer os.RemoveAll(tempDir)
c, err := New(filepath.Join(tempDir, "Db"))
if err != nil {
t.Fatalf("New leveldb,: %v", err)
}
cache.PleaseCache(t, c)
}