NewCache creates a new cache instance
(cacheFile, configPath string, sqlDebug bool)
| 49 | |
| 50 | // NewCache creates a new cache instance |
| 51 | func NewCache(cacheFile, configPath string, sqlDebug bool) (*Cache, error) { |
| 52 | Log.Debugf("Opening cache connection") |
| 53 | |
| 54 | db, err := bolt.Open(cacheFile, 0600, nil) |
| 55 | if nil != err { |
| 56 | Log.Debugf("%v", err) |
| 57 | return nil, fmt.Errorf("Could not open cache file") |
| 58 | } |
| 59 | |
| 60 | cache := Cache{ |
| 61 | db: db, |
| 62 | tokenPath: filepath.Join(configPath, "token.json"), |
| 63 | } |
| 64 | |
| 65 | // Make sure the necessary buckets exist |
| 66 | err = db.Update(func(tx *bolt.Tx) error { |
| 67 | if _, err := tx.CreateBucketIfNotExists(bObjects); nil != err { |
| 68 | return err |
| 69 | } |
| 70 | if _, err := tx.CreateBucketIfNotExists(bParents); nil != err { |
| 71 | return err |
| 72 | } |
| 73 | if _, err := tx.CreateBucketIfNotExists(bPageToken); nil != err { |
| 74 | return err |
| 75 | } |
| 76 | return nil |
| 77 | }) |
| 78 | |
| 79 | return &cache, err |
| 80 | } |
| 81 | |
| 82 | // Close closes all handles |
| 83 | func (c *Cache) Close() error { |