@@ -353,6 +353,7 @@ def __init__(
353
353
354
354
# Cache of existing indexes used by ensure_index ops.
355
355
self .__index_cache = {}
356
+ self .__index_cache_lock = threading .Lock ()
356
357
357
358
super (MongoClient , self ).__init__ (options .codec_options ,
358
359
options .read_preference ,
@@ -433,27 +434,29 @@ def _cached(self, dbname, coll, index):
433
434
"""Test if `index` is cached."""
434
435
cache = self .__index_cache
435
436
now = datetime .datetime .utcnow ()
436
- return (dbname in cache and
437
- coll in cache [dbname ] and
438
- index in cache [dbname ][coll ] and
439
- now < cache [dbname ][coll ][index ])
437
+ with self .__index_cache_lock :
438
+ return (dbname in cache and
439
+ coll in cache [dbname ] and
440
+ index in cache [dbname ][coll ] and
441
+ now < cache [dbname ][coll ][index ])
440
442
441
443
def _cache_index (self , dbname , collection , index , cache_for ):
442
444
"""Add an index to the index cache for ensure_index operations."""
443
445
now = datetime .datetime .utcnow ()
444
446
expire = datetime .timedelta (seconds = cache_for ) + now
445
447
446
- if database not in self .__index_cache :
447
- self .__index_cache [dbname ] = {}
448
- self .__index_cache [dbname ][collection ] = {}
449
- self .__index_cache [dbname ][collection ][index ] = expire
448
+ with self .__index_cache_lock :
449
+ if database not in self .__index_cache :
450
+ self .__index_cache [dbname ] = {}
451
+ self .__index_cache [dbname ][collection ] = {}
452
+ self .__index_cache [dbname ][collection ][index ] = expire
450
453
451
- elif collection not in self .__index_cache [dbname ]:
452
- self .__index_cache [dbname ][collection ] = {}
453
- self .__index_cache [dbname ][collection ][index ] = expire
454
+ elif collection not in self .__index_cache [dbname ]:
455
+ self .__index_cache [dbname ][collection ] = {}
456
+ self .__index_cache [dbname ][collection ][index ] = expire
454
457
455
- else :
456
- self .__index_cache [dbname ][collection ][index ] = expire
458
+ else :
459
+ self .__index_cache [dbname ][collection ][index ] = expire
457
460
458
461
def _purge_index (self , database_name ,
459
462
collection_name = None , index_name = None ):
@@ -463,22 +466,23 @@ def _purge_index(self, database_name,
463
466
464
467
If `collection_name` is None purge an entire database.
465
468
"""
466
- if not database_name in self .__index_cache :
467
- return
469
+ with self .__index_cache_lock :
470
+ if not database_name in self .__index_cache :
471
+ return
468
472
469
- if collection_name is None :
470
- del self .__index_cache [database_name ]
471
- return
473
+ if collection_name is None :
474
+ del self .__index_cache [database_name ]
475
+ return
472
476
473
- if not collection_name in self .__index_cache [database_name ]:
474
- return
477
+ if not collection_name in self .__index_cache [database_name ]:
478
+ return
475
479
476
- if index_name is None :
477
- del self .__index_cache [database_name ][collection_name ]
478
- return
480
+ if index_name is None :
481
+ del self .__index_cache [database_name ][collection_name ]
482
+ return
479
483
480
- if index_name in self .__index_cache [database_name ][collection_name ]:
481
- del self .__index_cache [database_name ][collection_name ][index_name ]
484
+ if index_name in self .__index_cache [database_name ][collection_name ]:
485
+ del self .__index_cache [database_name ][collection_name ][index_name ]
482
486
483
487
def _server_property (self , attr_name ):
484
488
"""An attribute of the current server's description.
0 commit comments