mirror of
https://github.com/mickael-kerjean/filestash
synced 2025-12-06 08:22:24 +01:00
fix (plg_backend_psql): caching issue
This commit is contained in:
parent
d1b993a028
commit
a83aed5a4c
4 changed files with 7 additions and 21 deletions
|
|
@ -11,8 +11,6 @@ import (
|
|||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
var PGCache AppCache
|
||||
|
||||
type PSQL struct {
|
||||
db *sql.DB
|
||||
ctx context.Context
|
||||
|
|
@ -20,23 +18,9 @@ type PSQL struct {
|
|||
|
||||
func init() {
|
||||
Backend.Register("psql", PSQL{})
|
||||
|
||||
PGCache = NewAppCache(2, 1)
|
||||
PGCache.OnEvict(func(key string, value interface{}) {
|
||||
c := value.(*PSQL)
|
||||
c.Close()
|
||||
})
|
||||
}
|
||||
|
||||
func (this PSQL) Init(params map[string]string, app *App) (IBackend, error) {
|
||||
if d := PGCache.Get(params); d != nil {
|
||||
backend := d.(*PSQL)
|
||||
if backend.db.Ping() == nil {
|
||||
backend.ctx = app.Context
|
||||
return backend, nil
|
||||
}
|
||||
PGCache.Del(params)
|
||||
}
|
||||
host := params["host"]
|
||||
port := withDefault(params["port"], "5432")
|
||||
user := params["user"]
|
||||
|
|
@ -64,7 +48,6 @@ func (this PSQL) Init(params map[string]string, app *App) (IBackend, error) {
|
|||
db: db,
|
||||
ctx: app.Context,
|
||||
}
|
||||
PGCache.Set(params, backend)
|
||||
return backend, nil
|
||||
}
|
||||
|
||||
|
|
@ -118,6 +101,7 @@ func (this PSQL) LoginForm() Form {
|
|||
}
|
||||
|
||||
func (this PSQL) Touch(path string) error {
|
||||
defer this.Close()
|
||||
if !strings.HasSuffix(path, ".form") {
|
||||
return ErrNotValid
|
||||
}
|
||||
|
|
@ -125,14 +109,17 @@ func (this PSQL) Touch(path string) error {
|
|||
}
|
||||
|
||||
func (this PSQL) Rm(path string) error {
|
||||
defer this.Close()
|
||||
return ErrNotAuthorized
|
||||
}
|
||||
|
||||
func (this PSQL) Mkdir(path string) error {
|
||||
defer this.Close()
|
||||
return ErrNotValid
|
||||
}
|
||||
|
||||
func (this PSQL) Mv(from string, to string) error {
|
||||
defer this.Close()
|
||||
return ErrNotValid
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
)
|
||||
|
||||
func (this PSQL) Cat(path string) (io.ReadCloser, error) {
|
||||
defer this.Close()
|
||||
l, err := getPath(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
)
|
||||
|
||||
func (this PSQL) Ls(path string) ([]os.FileInfo, error) {
|
||||
defer this.Close()
|
||||
l, err := getPath(path)
|
||||
if err != nil {
|
||||
Log.Debug("pl_backend_psql::ls method=getPath err=%s", err.Error())
|
||||
|
|
@ -39,12 +40,10 @@ func (this PSQL) Ls(path string) ([]os.FileInfo, error) {
|
|||
} else if l.row == "" {
|
||||
_, key, err := processTable(this.ctx, this.db, l.table)
|
||||
if err != nil {
|
||||
Log.Debug("plg_backend_psql::ls method=processTable err=%s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
rows, err := this.db.QueryContext(this.ctx, `SELECT "`+key+`" FROM "`+l.table+`" LIMIT 500000`)
|
||||
if err != nil {
|
||||
Log.Debug("plg_backend_psql::ls method=query err=%s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
|
@ -52,7 +51,6 @@ func (this PSQL) Ls(path string) ([]os.FileInfo, error) {
|
|||
for rows.Next() {
|
||||
var name string
|
||||
if err := rows.Scan(&name); err != nil {
|
||||
Log.Debug("plg_backend_psql::ls method=scan err=%s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, File{
|
||||
|
|
@ -62,6 +60,5 @@ func (this PSQL) Ls(path string) ([]os.FileInfo, error) {
|
|||
}
|
||||
return out, nil
|
||||
}
|
||||
Log.Stdout("plg_backend_psql::ls err=invalid location=%v", l)
|
||||
return []os.FileInfo{}, ErrNotValid
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import (
|
|||
)
|
||||
|
||||
func (this PSQL) Save(path string, file io.Reader) error {
|
||||
defer this.Close()
|
||||
l, err := getPath(path)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
Loading…
Reference in a new issue