sm
/
cache
1
0
Fork 0
This commit is contained in:
Diogo Franco 2014-12-11 08:42:07 +00:00
commit 0386c8ca8d
1 changed files with 12 additions and 8 deletions

View File

@ -869,24 +869,22 @@ func (c *cache) DeleteExpired() {
}
// 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) {
enc := gob.NewEncoder(w)
defer func() {
if x := recover(); x != nil {
err = fmt.Errorf("Error registering item types with Gob library")
}
}()
c.RLock()
defer c.RUnlock()
for _, v := range c.items {
gob.Register(v.Object)
}
err = enc.Encode(&c.items)
return
}
// Save the cache's items to the given filename, creating the file if it
// 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 {
fp, err := os.Create(fname)
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
// 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 {
dec := gob.NewDecoder(r)
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
// 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 {
fp, err := os.Open(fname)
if err != nil {