feat: hltb plugin
This commit is contained in:
parent
c53942ac53
commit
f13598a329
24 changed files with 940 additions and 17 deletions
|
@ -793,3 +793,56 @@ func initDatabase(db *sql.DB) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CacheGet retrieves a value from the cache
|
||||
func (d *Database) CacheGet(key string) (string, error) {
|
||||
query := `
|
||||
SELECT value
|
||||
FROM cache
|
||||
WHERE key = ? AND (expires_at IS NULL OR expires_at > ?)
|
||||
`
|
||||
|
||||
var value string
|
||||
err := d.db.QueryRow(query, key, time.Now()).Scan(&value)
|
||||
if err == sql.ErrNoRows {
|
||||
return "", ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// CacheSet stores a value in the cache with optional expiration
|
||||
func (d *Database) CacheSet(key, value string, expiration *time.Time) error {
|
||||
query := `
|
||||
INSERT OR REPLACE INTO cache (key, value, expires_at, updated_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`
|
||||
|
||||
_, err := d.db.Exec(query, key, value, expiration, time.Now())
|
||||
return err
|
||||
}
|
||||
|
||||
// CacheDelete removes a value from the cache
|
||||
func (d *Database) CacheDelete(key string) error {
|
||||
query := `
|
||||
DELETE FROM cache
|
||||
WHERE key = ?
|
||||
`
|
||||
|
||||
_, err := d.db.Exec(query, key)
|
||||
return err
|
||||
}
|
||||
|
||||
// CacheCleanup removes expired cache entries
|
||||
func (d *Database) CacheCleanup() error {
|
||||
query := `
|
||||
DELETE FROM cache
|
||||
WHERE expires_at IS NOT NULL AND expires_at <= ?
|
||||
`
|
||||
|
||||
_, err := d.db.Exec(query, time.Now())
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue