|
| 1 | +package dbcache |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/bhmj/goblocks/dbase/abstract" |
| 8 | + "github.com/bhmj/goblocks/file" |
| 9 | + "github.com/bhmj/goblocks/log" |
| 10 | + "github.com/bhmj/goblocks/www" |
| 11 | +) |
| 12 | + |
| 13 | +type cache struct { |
| 14 | + db abstract.DB |
| 15 | + logger log.MetaLogger |
| 16 | + cacheDir string |
| 17 | +} |
| 18 | + |
| 19 | +type Cache interface { |
| 20 | + GetURL(url string) (string, error) |
| 21 | + GetContent(url string) ([]byte, string, error) |
| 22 | + Cleanup() |
| 23 | +} |
| 24 | + |
| 25 | +func New(db abstract.DB, logger log.MetaLogger, cacheDir string) Cache { |
| 26 | + return &cache{ |
| 27 | + db: db, |
| 28 | + logger: logger, |
| 29 | + cacheDir: cacheDir, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (c *cache) GetURL(url string) (extPath string, err error) { |
| 34 | + extPath, _ = c.getCacheRecord(url) |
| 35 | + fullPath := filepath.Join(c.cacheDir, extPath) |
| 36 | + if extPath != "" && file.Exists(fullPath) { |
| 37 | + return |
| 38 | + } |
| 39 | + extPath, contentType, fileSize, err := c.requestURL(url) |
| 40 | + if err != nil { |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + err = c.setCacheRecord(url, extPath, contentType, fileSize) |
| 45 | + return |
| 46 | +} |
| 47 | + |
| 48 | +func (c *cache) GetContent(url string) (body []byte, contentType string, err error) { |
| 49 | + extPath, contentType := c.getCacheRecord(url) |
| 50 | + fullPath := filepath.Join(c.cacheDir, extPath) |
| 51 | + if extPath != "" && file.Exists(fullPath) { |
| 52 | + body, err = file.Read(fullPath) |
| 53 | + return |
| 54 | + } |
| 55 | + extPath, body, contentType, fileSize, err := c.fetchURL(url) |
| 56 | + if err != nil { |
| 57 | + return |
| 58 | + } |
| 59 | + err = c.setCacheRecord(url, extPath, contentType, fileSize) |
| 60 | + return |
| 61 | +} |
| 62 | + |
| 63 | +type cacheRec struct { |
| 64 | + FilePath string `db:"file_path"` |
| 65 | + ContentType string `db:"content_type"` |
| 66 | + AddedAt time.Time `db:"added_at"` |
| 67 | +} |
| 68 | + |
| 69 | +func (c *cache) getCacheRecord(url string) (string, string) { |
| 70 | + // TODO: add memory cache |
| 71 | + var entry cacheRec |
| 72 | + sql := ` |
| 73 | + with upd as ( |
| 74 | + update file_cache set |
| 75 | + last_read_at = now() |
| 76 | + where source_url = $1 |
| 77 | + returning id |
| 78 | + ) |
| 79 | + select file_path, content_type, added_at |
| 80 | + from file_cache |
| 81 | + where id = (select id from upd limit 1)` |
| 82 | + found, err := c.db.QueryRow(&entry, sql, url) |
| 83 | + if err != nil { |
| 84 | + c.logger.Error("getting cache record", log.Error(err)) |
| 85 | + return "", "" |
| 86 | + } |
| 87 | + if !found { |
| 88 | + c.logger.Info("getting cache record: not found", log.String("url", url)) |
| 89 | + } |
| 90 | + return entry.FilePath, entry.ContentType |
| 91 | +} |
| 92 | + |
| 93 | +func (c *cache) setCacheRecord(url, extPath, contentType string, fileSize int64) error { |
| 94 | + // TODO: update memory cache |
| 95 | + sql := ` |
| 96 | + insert into file_cache ( |
| 97 | + source_url, file_path, content_type, file_size |
| 98 | + ) values ( |
| 99 | + $1, $2, $3, $4 |
| 100 | + ) on conflict (source_url) do update set |
| 101 | + file_path = excluded.file_path, |
| 102 | + content_type = excluded.content_type |
| 103 | + ;` |
| 104 | + return c.db.Exec(sql, url, extPath, contentType, fileSize) |
| 105 | +} |
| 106 | + |
| 107 | +func (c *cache) contentTypeUpdate(url, contentType string, fileSize int64) { |
| 108 | + sql := `update file_cache set content_type = $1, file_size = $2 where source_url = $3` |
| 109 | + err := c.db.Exec(sql, contentType, fileSize, url) |
| 110 | + if err != nil { |
| 111 | + c.logger.Error("updating content_type", log.Error(err)) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func (c *cache) requestURL(url string) (string, string, int64, error) { |
| 116 | + path := time.Now().Format("2006-01-02") |
| 117 | + return www.EnqueueDownload(url, c.cacheDir, path, c.contentTypeUpdate) |
| 118 | +} |
| 119 | + |
| 120 | +func (c *cache) fetchURL(url string) (string, []byte, string, int64, error) { |
| 121 | + path := time.Now().Format("2006-01-02") |
| 122 | + return www.DownloadContent(url, c.cacheDir, path) |
| 123 | +} |
| 124 | + |
| 125 | +func (c *cache) Cleanup() {} |
0 commit comments