cache special drive items until space root changes

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
Jörn Friedrich Dreyer
2023-05-02 11:44:27 +02:00
parent 332593d8bb
commit ca638ddc51
5 changed files with 40 additions and 5 deletions
@@ -54,6 +54,8 @@ func DefaultConfig() *config.Config {
WebDavPath: "/dav/spaces/", WebDavPath: "/dav/spaces/",
DefaultQuota: "1000000000", DefaultQuota: "1000000000",
// 30 minutes // 30 minutes
ExtendedSpacePropertiesCacheTTL: 1800,
// 30 minutes
GroupsCacheTTL: 1800, GroupsCacheTTL: 1800,
// 30 minutes // 30 minutes
UsersCacheTTL: 1800, UsersCacheTTL: 1800,
+35 -2
View File
@@ -215,8 +215,20 @@ func (g Graph) getPathForResource(ctx context.Context, id storageprovider.Resour
return res.Path, err return res.Path, err
} }
// getExtendedSpaceProperties reads properties from the opaque and transforms them into driveItems // getSpecialDriveItems reads properties from the opaque and transforms them into driveItems
func (g Graph) getExtendedSpaceProperties(ctx context.Context, baseURL *url.URL, space *storageprovider.StorageSpace) []libregraph.DriveItem { func (g Graph) getSpecialDriveItems(ctx context.Context, baseURL *url.URL, space *storageprovider.StorageSpace) []libregraph.DriveItem {
// if the root is older or equal to our cache we can reuse the cached extended spaces properties
if entry := g.specialDriveItemsCache.Get(spaceRootStatKey(space.Root)); entry != nil {
if spe, ok := entry.Value().(specialDriveItemEntry); ok {
if spe.rootMtime != nil && space.Mtime != nil {
if spe.rootMtime.Seconds >= space.Mtime.Seconds { // second precision is good enough
return spe.specialDriveItems
}
}
}
}
var spaceItems []libregraph.DriveItem var spaceItems []libregraph.DriveItem
if space.Opaque == nil { if space.Opaque == nil {
return nil return nil
@@ -235,9 +247,30 @@ func (g Graph) getExtendedSpaceProperties(ctx context.Context, baseURL *url.URL,
} }
} }
} }
// cache properties
spacePropertiesEntry := specialDriveItemEntry{
specialDriveItems: spaceItems,
rootMtime: space.Mtime,
}
g.specialDriveItemsCache.Set(spaceRootStatKey(space.Root), spacePropertiesEntry, time.Duration(g.config.Spaces.ExtendedSpacePropertiesCacheTTL))
return spaceItems return spaceItems
} }
// generates a space root stat cache key used to detect changes in a space
func spaceRootStatKey(id *storageprovider.ResourceId) string {
if id == nil {
return ""
}
return id.StorageId + "$" + id.SpaceId + "!" + id.OpaqueId
}
type specialDriveItemEntry struct {
specialDriveItems []libregraph.DriveItem
rootMtime *types.Timestamp
}
func (g Graph) getSpecialDriveItem(ctx context.Context, id storageprovider.ResourceId, itemName string, baseURL *url.URL, space *storageprovider.StorageSpace) *libregraph.DriveItem { func (g Graph) getSpecialDriveItem(ctx context.Context, id storageprovider.ResourceId, itemName string, baseURL *url.URL, space *storageprovider.StorageSpace) *libregraph.DriveItem {
var spaceItem *libregraph.DriveItem var spaceItem *libregraph.DriveItem
if id.SpaceId == "" && id.OpaqueId == "" { if id.SpaceId == "" && id.OpaqueId == "" {
+1 -1
View File
@@ -536,7 +536,7 @@ func (g Graph) formatDrives(ctx context.Context, baseURL *url.URL, storageSpaces
// can't access disabled space // can't access disabled space
if utils.ReadPlainFromOpaque(storageSpace.Opaque, "trashed") != "trashed" { if utils.ReadPlainFromOpaque(storageSpace.Opaque, "trashed") != "trashed" {
res.Special = g.getExtendedSpaceProperties(ctx, baseURL, storageSpace) res.Special = g.getSpecialDriveItems(ctx, baseURL, storageSpace)
quota, err := g.getDriveQuota(ctx, storageSpace) quota, err := g.getDriveQuota(ctx, storageSpace)
res.Quota = &quota res.Quota = &quota
if err != nil { if err != nil {
+1 -1
View File
@@ -65,7 +65,7 @@ type Graph struct {
gatewayClient gateway.GatewayAPIClient gatewayClient gateway.GatewayAPIClient
roleService RoleService roleService RoleService
permissionsService Permissions permissionsService Permissions
spacePropertiesCache *ttlcache.Cache[string, interface{}] specialDriveItemsCache *ttlcache.Cache[string, interface{}]
usersCache *ttlcache.Cache[string, libregraph.User] usersCache *ttlcache.Cache[string, libregraph.User]
groupsCache *ttlcache.Cache[string, libregraph.Group] groupsCache *ttlcache.Cache[string, libregraph.Group]
eventsPublisher events.Publisher eventsPublisher events.Publisher
+1 -1
View File
@@ -136,7 +136,7 @@ func NewService(opts ...Option) (Graph, error) {
config: options.Config, config: options.Config,
mux: m, mux: m,
logger: &options.Logger, logger: &options.Logger,
spacePropertiesCache: spacePropertiesCache, specialDriveItemsCache: spacePropertiesCache,
usersCache: usersCache, usersCache: usersCache,
groupsCache: groupsCache, groupsCache: groupsCache,
eventsPublisher: options.EventsPublisher, eventsPublisher: options.EventsPublisher,