@@ -62,25 +62,45 @@ func GetMemoise(params apiRouter.ApiHandlerGenericParams) error {
6262 return err
6363 }
6464
65- // Update last accessed time here
66- timestamp := uint32 (params .Svcs .TimeStamper .GetTimeNowSec ())
67- if timestamp != item .LastReadTimeUnixSec {
68- update := bson.D {{Key : "$set" , Value : bson.D {{Key : "lastreadtimeunixsec" , Value : timestamp }}}}
69- updResult , err := coll .UpdateOne (ctx , filter , update , options .Update ())
65+ now := uint32 (params .Svcs .TimeStamper .GetTimeNowSec ())
66+
67+ // Check if this is passed the max age we allow for an item to live in our cache
68+ if item .LastReadTimeUnixSec < now - uint32 (params .Svcs .Config .MaxUnretrievedMemoisationAgeSec ) {
69+ // It's too old, delete & don't return
70+ params .Svcs .Log .Infof ("Retrieved memoised item: %v that hasn't been accessed in %v sec. Deleting." , key , now - item .LastReadTimeUnixSec )
71+
72+ delResult , err := coll .DeleteOne (ctx , filter , options .Delete ())
7073 if err != nil {
7174 // Don't error out on this, but do notify
72- params .Svcs .Log .Errorf ("Failed to update last read time stamp for memoised item: %v. Error: %v" , key , err )
73- }
75+ params .Svcs .Log .Errorf ("Failed to delete outdated memoised item: %v. Error: %v" , key , err )
76+ } else {
77+ if delResult .DeletedCount != 1 {
78+ params .Svcs .Log .Errorf ("Memoised item delete had unexpected counts %+v key: %v" , delResult , key )
79+ }
7480
75- if updResult .ModifiedCount != 1 {
76- params .Svcs .Log .Errorf ("Memoised item timestamp update had unexpected counts %+v key: %v" , updResult , key )
81+ return errorwithstatus .MakeNotFoundError (key )
82+ }
83+ } else {
84+ // Update last accessed time here
85+ if now != item .LastReadTimeUnixSec {
86+ update := bson.D {{Key : "$set" , Value : bson.D {{Key : "lastreadtimeunixsec" , Value : now }}}}
87+ updResult , err := coll .UpdateOne (ctx , filter , update , options .Update ())
88+ if err != nil {
89+ // Don't error out on this, but do notify
90+ params .Svcs .Log .Errorf ("Failed to update last read time stamp for memoised item: %v. Error: %v" , key , err )
91+ }
92+
93+ if updResult .ModifiedCount != 1 {
94+ params .Svcs .Log .Errorf ("Memoised item timestamp update had unexpected counts %+v key: %v" , updResult , key )
95+ }
96+
97+ // Also set it in the item we're replying with
98+ item .LastReadTimeUnixSec = now
7799 }
78-
79- // Also set it in the item we're replying with
80- item .LastReadTimeUnixSec = timestamp
81100 }
82101
83102 utils .SendProtoBinary (params .Writer , item )
103+
84104 return nil
85105}
86106
0 commit comments