mirror of
https://github.com/safing/portbase
synced 2025-09-01 18:19:57 +00:00
Fix locking when updating the interface cache in Put methods
Also, update the cache when fetching records from the database.
This commit is contained in:
parent
85e985c493
commit
ac1e00b0fe
2 changed files with 27 additions and 8 deletions
|
@ -188,6 +188,16 @@ func (i *Interface) getRecord(dbName string, dbKey string, mustBeWriteable bool)
|
||||||
return nil, db, ErrPermissionDenied
|
return nil, db, ErrPermissionDenied
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.Lock()
|
||||||
|
ttl := r.Meta().GetRelativeExpiry()
|
||||||
|
r.Unlock()
|
||||||
|
i.updateCache(
|
||||||
|
r,
|
||||||
|
false, // writing
|
||||||
|
false, // remove
|
||||||
|
ttl, // expiry
|
||||||
|
)
|
||||||
|
|
||||||
return r, db, nil
|
return r, db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -243,14 +253,19 @@ func (i *Interface) Put(r record.Record) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Lock()
|
r.Lock()
|
||||||
defer r.Unlock()
|
|
||||||
i.options.Apply(r)
|
i.options.Apply(r)
|
||||||
|
remove := r.Meta().IsDeleted()
|
||||||
|
ttl := r.Meta().GetRelativeExpiry()
|
||||||
|
r.Unlock()
|
||||||
|
|
||||||
written := i.updateCache(r, true)
|
// The record may not be locked when updating the cache.
|
||||||
|
written := i.updateCache(r, true, remove, ttl)
|
||||||
if written {
|
if written {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.Lock()
|
||||||
|
defer r.Unlock()
|
||||||
return db.Put(r)
|
return db.Put(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -271,18 +286,22 @@ func (i *Interface) PutNew(r record.Record) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Lock()
|
r.Lock()
|
||||||
defer r.Unlock()
|
|
||||||
|
|
||||||
if r.Meta() != nil {
|
if r.Meta() != nil {
|
||||||
r.Meta().Reset()
|
r.Meta().Reset()
|
||||||
}
|
}
|
||||||
i.options.Apply(r)
|
i.options.Apply(r)
|
||||||
|
remove := r.Meta().IsDeleted()
|
||||||
|
ttl := r.Meta().GetRelativeExpiry()
|
||||||
|
r.Unlock()
|
||||||
|
|
||||||
written := i.updateCache(r, true)
|
// The record may not be locked when updating the cache.
|
||||||
|
written := i.updateCache(r, true, remove, ttl)
|
||||||
if written {
|
if written {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.Lock()
|
||||||
|
defer r.Unlock()
|
||||||
return db.Put(r)
|
return db.Put(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -159,14 +159,15 @@ func (i *Interface) checkCache(key string) record.Record {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interface) updateCache(r record.Record, write bool) (written bool) {
|
// updateCache updates an entry in the
|
||||||
|
func (i *Interface) updateCache(r record.Record, write bool, remove bool, ttl int64) (written bool) {
|
||||||
// Check if cache is in use.
|
// Check if cache is in use.
|
||||||
if i.cache == nil {
|
if i.cache == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if record should be deleted
|
// Check if record should be deleted
|
||||||
if r.Meta().IsDeleted() {
|
if remove {
|
||||||
// Remove entry from cache.
|
// Remove entry from cache.
|
||||||
i.cache.Remove(r.Key())
|
i.cache.Remove(r.Key())
|
||||||
// Let write through to database storage.
|
// Let write through to database storage.
|
||||||
|
@ -174,7 +175,6 @@ func (i *Interface) updateCache(r record.Record, write bool) (written bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update cache with record.
|
// Update cache with record.
|
||||||
ttl := r.Meta().GetRelativeExpiry()
|
|
||||||
if ttl >= 0 {
|
if ttl >= 0 {
|
||||||
_ = i.cache.SetWithExpire(
|
_ = i.cache.SetWithExpire(
|
||||||
r.Key(),
|
r.Key(),
|
||||||
|
|
Loading…
Add table
Reference in a new issue