Merge abe7c4fb56
into 7d1d6d6ae9
This commit is contained in:
commit
0386c8ca8d
20
cache.go
20
cache.go
|
@ -869,24 +869,22 @@ func (c *cache) DeleteExpired() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the cache's items (using Gob) to an io.Writer.
|
// Write the cache's items (using Gob) to an io.Writer.
|
||||||
|
//
|
||||||
|
// The caller should register any custom types with encoding/gob.Register
|
||||||
|
// before calling this function.
|
||||||
func (c *cache) Save(w io.Writer) (err error) {
|
func (c *cache) Save(w io.Writer) (err error) {
|
||||||
enc := gob.NewEncoder(w)
|
enc := gob.NewEncoder(w)
|
||||||
defer func() {
|
|
||||||
if x := recover(); x != nil {
|
|
||||||
err = fmt.Errorf("Error registering item types with Gob library")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
c.RLock()
|
c.RLock()
|
||||||
defer c.RUnlock()
|
defer c.RUnlock()
|
||||||
for _, v := range c.items {
|
|
||||||
gob.Register(v.Object)
|
|
||||||
}
|
|
||||||
err = enc.Encode(&c.items)
|
err = enc.Encode(&c.items)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the cache's items to the given filename, creating the file if it
|
// Save the cache's items to the given filename, creating the file if it
|
||||||
// doesn't exist, and overwriting it if it does.
|
// doesn't exist, and overwriting it if it does.
|
||||||
|
//
|
||||||
|
// The caller should register any custom types with encoding/gob.Register
|
||||||
|
// before calling this function.
|
||||||
func (c *cache) SaveFile(fname string) error {
|
func (c *cache) SaveFile(fname string) error {
|
||||||
fp, err := os.Create(fname)
|
fp, err := os.Create(fname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -902,6 +900,9 @@ func (c *cache) SaveFile(fname string) error {
|
||||||
|
|
||||||
// Add (Gob-serialized) cache items from an io.Reader, excluding any items with
|
// Add (Gob-serialized) cache items from an io.Reader, excluding any items with
|
||||||
// keys that already exist (and haven't expired) in the current cache.
|
// keys that already exist (and haven't expired) in the current cache.
|
||||||
|
//
|
||||||
|
// The caller should register any custom types with encoding/gob.Register
|
||||||
|
// before calling this function.
|
||||||
func (c *cache) Load(r io.Reader) error {
|
func (c *cache) Load(r io.Reader) error {
|
||||||
dec := gob.NewDecoder(r)
|
dec := gob.NewDecoder(r)
|
||||||
items := map[string]*Item{}
|
items := map[string]*Item{}
|
||||||
|
@ -921,6 +922,9 @@ func (c *cache) Load(r io.Reader) error {
|
||||||
|
|
||||||
// Load and add cache items from the given filename, excluding any items with
|
// Load and add cache items from the given filename, excluding any items with
|
||||||
// keys that already exist in the current cache.
|
// keys that already exist in the current cache.
|
||||||
|
//
|
||||||
|
// The caller should register any custom types with encoding/gob.Register
|
||||||
|
// before calling this function.
|
||||||
func (c *cache) LoadFile(fname string) error {
|
func (c *cache) LoadFile(fname string) error {
|
||||||
fp, err := os.Open(fname)
|
fp, err := os.Open(fname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue