Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 39156f7

Browse files
committed
api, webui: Clear cache usage limits when changing assigned limits
When changing the usage limits of a user via the web UI clear the cached usage limits. This makes sure the newly assigned limits are loaded at the time of the next API call. This means regular loading of usage data from the database is no longer necessary for applying newly assigned limits, so the time for reloading can be increased (or even reloading completely removed). Note this only works when the api and the webui backend share the same memcached instance.
1 parent ccc63ca commit 39156f7

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

api/limiter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const cacheTime int = 0
1616

1717
// Interval for flushing the cached data of a user and reloading it from the database
1818
// Reloading the data from database also reloads the assigned usage limits. So this is also the maximum
19-
// time a user has to wait until a newly assigned limit is active.
20-
const reloadInterval time.Duration = 1 * time.Hour
19+
// time a user has to wait until a newly assigned limit is active unless the cache is cleared.
20+
const reloadInterval time.Duration = 24 * time.Hour
2121

2222
type rateLimitCacheData struct {
2323
// These values reflect the applied settings from the usage_limits table

common/memcache.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/tls"
88
"encoding/gob"
99
"encoding/hex"
10+
"errors"
1011
"fmt"
1112
"io"
1213
"log"
@@ -80,6 +81,18 @@ func ConnectCache() (err error) {
8081
return nil
8182
}
8283

84+
// DeleteCacheItem deletes the cached item with the given key if it exists
85+
func DeleteCacheItem(cacheKey string) (error) {
86+
err := memCache.Delete(cacheKey)
87+
88+
// We don't care about cache misses
89+
if errors.Is(err, memcache.ErrCacheMiss) {
90+
return nil
91+
}
92+
93+
return err
94+
}
95+
8396
// GetCachedData retrieves cached data from Memcached
8497
func GetCachedData(cacheKey string, cacheData interface{}) (bool, error) {
8598
cacheItem, err := memCache.Get(cacheKey)

webui/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3992,6 +3992,13 @@ func saveLimitsHandler(w http.ResponseWriter, r *http.Request) {
39923992
return
39933993
}
39943994

3995+
// Flush cached limits for user so they are applied immediately
3996+
err = com.DeleteCacheItem("limits-" + username)
3997+
if err != nil {
3998+
w.WriteHeader(http.StatusInternalServerError)
3999+
return
4000+
}
4001+
39954002
return
39964003
}
39974004

0 commit comments

Comments
 (0)