Ejemplo n.º 1
0
// Retrieve returns a cached Resource for the given key
func (c *cache) Retrieve(key string) (*Resource, error) {
	f, err := c.fs.Open(bodyPrefix + formatPrefix + hashKey(key))
	if err != nil {
		if vfs.IsNotExist(err) {
			return nil, ErrNotFoundInCache
		}
		return nil, err
	}
	h, err := c.Header(key)
	if err != nil {
		if vfs.IsNotExist(err) {
			return nil, ErrNotFoundInCache
		}
		return nil, err
	}

	contentLength, err := f.Seek(0, 2)
	if err != nil {
		if vfs.IsNotExist(err) {
			return nil, ErrNotFoundInCache
		}
		return nil, err
	}
	f.Seek(0, 0)

	res := NewResource(h.StatusCode, contentLength, f, h.Header)
	if staleTime, exists := c.stale[key]; exists {
		if !res.DateAfter(staleTime) {
			log.Printf("stale marker of %s found", staleTime)
			res.MarkStale()
		}
	}
	return res, nil
}
Ejemplo n.º 2
0
// Retrieve the Status and Headers for a given key path
func (c *cache) Header(key string) (Header, error) {
	path := headerPrefix + formatPrefix + hashKey(key)
	f, err := c.fs.Open(path)
	if err != nil {
		if vfs.IsNotExist(err) {
			return Header{}, ErrNotFoundInCache
		}
		return Header{}, err
	}

	return readHeaders(bufio.NewReader(f))
}
Ejemplo n.º 3
0
func (m *Manager) Create(name string, overwrite bool) (io.WriteCloser, error) {
	flags := os.O_WRONLY | os.O_CREATE
	if !overwrite {
		flags |= os.O_EXCL
	}
	f, err := m.fs.OpenFile(name, flags, 0644)
	if err != nil && vfs.IsNotExist(err) {
		// Try to create the directory
		if err2 := vfs.MkdirAll(m.fs, path.Dir(name), 0755); err2 == nil {
			f, err = m.fs.OpenFile(name, flags, 0644)
		}
	}
	return f, err
}