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-04-28 13:50:59 +02:00
parent 332593d8bb
commit ca638ddc51
5 changed files with 40 additions and 5 deletions

View File

@@ -54,6 +54,8 @@ func DefaultConfig() *config.Config {
WebDavPath: "/dav/spaces/",
DefaultQuota: "1000000000",
// 30 minutes
ExtendedSpacePropertiesCacheTTL: 1800,
// 30 minutes
GroupsCacheTTL: 1800,
// 30 minutes
UsersCacheTTL: 1800,

View File

@@ -215,8 +215,20 @@ func (g Graph) getPathForResource(ctx context.Context, id storageprovider.Resour
return res.Path, err
}
// getExtendedSpaceProperties 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 {
// getSpecialDriveItems reads properties from the opaque and transforms them into driveItems
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
if space.Opaque == 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
}
// 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 {
var spaceItem *libregraph.DriveItem
if id.SpaceId == "" && id.OpaqueId == "" {

View File

@@ -536,7 +536,7 @@ func (g Graph) formatDrives(ctx context.Context, baseURL *url.URL, storageSpaces
// can't access disabled space
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)
res.Quota = &quota
if err != nil {

View File

@@ -65,7 +65,7 @@ type Graph struct {
gatewayClient gateway.GatewayAPIClient
roleService RoleService
permissionsService Permissions
spacePropertiesCache *ttlcache.Cache[string, interface{}]
specialDriveItemsCache *ttlcache.Cache[string, interface{}]
usersCache *ttlcache.Cache[string, libregraph.User]
groupsCache *ttlcache.Cache[string, libregraph.Group]
eventsPublisher events.Publisher

View File

@@ -136,7 +136,7 @@ func NewService(opts ...Option) (Graph, error) {
config: options.Config,
mux: m,
logger: &options.Logger,
spacePropertiesCache: spacePropertiesCache,
specialDriveItemsCache: spacePropertiesCache,
usersCache: usersCache,
groupsCache: groupsCache,
eventsPublisher: options.EventsPublisher,