mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
Rename storage certificate cache
This cache is used only for ACME certificates. Being explicit is always better.
This commit is contained in:
parent
0413daf76b
commit
f29940d784
2 changed files with 10 additions and 10 deletions
63
storage/certificate_cache.go
Normal file
63
storage/certificate_cache.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
// Copyright 2020 Dave Marquard. All rights reserved.
|
||||
// Use of this source code is governed by the Apache 2.0
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package storage // import "miniflux.app/storage"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"golang.org/x/crypto/acme/autocert"
|
||||
)
|
||||
|
||||
// Making sure that we're adhering to the autocert.Cache interface.
|
||||
var _ autocert.Cache = (*CertificateCache)(nil)
|
||||
|
||||
// CertificateCache provides a SQL backend to the autocert cache.
|
||||
type CertificateCache struct {
|
||||
storage *Storage
|
||||
}
|
||||
|
||||
// NewCertificateCache creates an cache instance that can be used with autocert.Cache.
|
||||
// It returns any errors that could happen while connecting to SQL.
|
||||
func NewCertificateCache(storage *Storage) *CertificateCache {
|
||||
return &CertificateCache{
|
||||
storage: storage,
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns a certificate data for the specified key.
|
||||
// If there's no such key, Get returns ErrCacheMiss.
|
||||
func (c *CertificateCache) Get(ctx context.Context, key string) ([]byte, error) {
|
||||
query := `SELECT data::bytea FROM acme_cache WHERE key = $1`
|
||||
var data []byte
|
||||
err := c.storage.db.QueryRowContext(ctx, query, key).Scan(&data)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, autocert.ErrCacheMiss
|
||||
}
|
||||
|
||||
return data, err
|
||||
}
|
||||
|
||||
// Put stores the data in the cache under the specified key.
|
||||
func (c *CertificateCache) Put(ctx context.Context, key string, data []byte) error {
|
||||
query := `INSERT INTO acme_cache (key, data, updated_at) VALUES($1, $2::bytea, now())
|
||||
ON CONFLICT (key) DO UPDATE SET data = $2::bytea, updated_at = now()`
|
||||
_, err := c.storage.db.ExecContext(ctx, query, key, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete removes a certificate data from the cache under the specified key.
|
||||
// If there's no such key in the cache, Delete returns nil.
|
||||
func (c *CertificateCache) Delete(ctx context.Context, key string) error {
|
||||
query := `DELETE FROM acme_cache WHERE key = $1`
|
||||
_, err := c.storage.db.ExecContext(ctx, query, key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue