diff --git a/lib/storage/metadata/mongoclient/MongoClientInterface.js b/lib/storage/metadata/mongoclient/MongoClientInterface.js index bfedee407..4c297fafe 100644 --- a/lib/storage/metadata/mongoclient/MongoClientInterface.js +++ b/lib/storage/metadata/mongoclient/MongoClientInterface.js @@ -53,6 +53,7 @@ const CONCURRENT_CURSORS = 10; const initialInstanceID = process.env.INITIAL_INSTANCE_ID; let uidCounter = 0; +let cachedBucketAttributes = null; const BUCKET_VERSIONS = require('../../../versioning/constants') .VersioningConstants.BucketVersioningKeyFormat; @@ -352,24 +353,47 @@ class MongoClientInterface { } getBucketAndObject(bucketName, objName, params, log, cb) { + // Check if the bucket attributes are already cached + if (cachedBucketAttributes) { + log.info('Using cached bucket attributes'); + return this.getObject(bucketName, objName, params, log, (err, obj) => { + if (err) { + if (err.is.NoSuchKey) { + return cb(null, { + bucket: BucketInfo.fromObj(cachedBucketAttributes).serialize(), + }); + } + log.error('getObject: error getting object', { error: err.message }); + return cb(err); + } + return cb(null, { + bucket: BucketInfo.fromObj(cachedBucketAttributes).serialize(), + obj: JSON.stringify(obj), + }); + }); + } + + // If no cached attributes, call getBucketAttributes and cache the result this.getBucketAttributes(bucketName, log, (err, bucket) => { if (err) { log.error( 'getBucketAttributes: error getting bucket attributes', - { error: err.message }); + { error: err.message } + ); return cb(err); } + + // Cache the bucket attributes + cachedBucketAttributes = bucket; + this.getObject(bucketName, objName, params, log, (err, obj) => { if (err) { if (err.is.NoSuchKey) { - return cb(null, - { - bucket: - BucketInfo.fromObj(bucket).serialize(), - }); + return cb(null, { + bucket: BucketInfo.fromObj(bucket).serialize(), + }); } - log.error('getObject: error getting object', - { error: err.message }); + log.error('getObject: error getting object', { error: err.message }); return cb(err); } return cb(null, {