16 // Creates (or opens) a CacheDir using the given path.
17 func NewCacheDir(path string) (cd *CacheDir, err error) {
19 compressionLevel: DefaultCompressionLevel,
21 if err = cd.SetCacheDir(path); err != nil {
27 // Sets the directory that will back this cache.
29 // Will try to os.MkdirAll the given path; if that fails,
30 // then the CacheDir is not modified.
31 func (cd *CacheDir) SetCacheDir(path string) (err error) {
33 defer cd.mutex.Unlock()
35 path = filepath.Join(filterDotsAll(filepath.SplitList(path)...)...)
37 if err = os.MkdirAll(path, 0777); err != nil {
44 // Gets the path to the cache directory.
45 func (cd *CacheDir) GetCacheDir() string {
47 defer cd.mutex.RUnlock()
52 // Opens the file that backs the specified key.
53 func (cd *CacheDir) Open(key ...CacheKey) (fh *os.File, err error) {
54 return os.Open(cd.cachePath(key...))
57 // Opens the file that backs the specified key using os.OpenFile.
59 // The permission bits are always 0666, which then get filtered by umask.
60 func (cd *CacheDir) OpenFlags(flags int, key ...CacheKey) (fh *os.File, err error) {
61 return os.OpenFile(cd.cachePath(key...), flags, 0666)
64 // Creates a new file to back the specified key.
65 func (cd *CacheDir) Create(key ...CacheKey) (fh *os.File, err error) {
66 subItem := cd.cachePath(key...)
67 subDir := filepath.Dir(subItem)
69 if err = os.MkdirAll(subDir, 0777); err != nil {
72 return os.Create(subItem)
75 // Deletes the file that backs the specified key.
76 func (cd *CacheDir) Delete(key ...CacheKey) (err error) {
77 return os.Remove(cd.cachePath(key...))
80 // Deletes the specified key and all subkeys.
81 func (cd *CacheDir) DeleteAll(key ...CacheKey) (err error) {
82 return os.RemoveAll(cd.cachePath(key...))