@@ -107,18 +107,6 @@ def delay_denial(func):
107
107
return func
108
108
109
109
110
- def get_account_memcache_key (account ):
111
- cache_key , env_key = _get_cache_key (account , None )
112
- return cache_key
113
-
114
-
115
- def get_container_memcache_key (account , container ):
116
- if not container :
117
- raise ValueError ("container not provided" )
118
- cache_key , env_key = _get_cache_key (account , container )
119
- return cache_key
120
-
121
-
122
110
def _prep_headers_to_info (headers , server_type ):
123
111
"""
124
112
Helper method that iterates once over a dict of headers,
@@ -424,40 +412,32 @@ def get_account_info(env, app, swift_source=None):
424
412
return info
425
413
426
414
427
- def _get_cache_key (account , container ):
415
+ def get_cache_key (account , container = None , obj = None ):
428
416
"""
429
- Get the keys for both memcache (cache_key) and env (env_key )
430
- where info about accounts and containers is cached
417
+ Get the keys for both memcache and env['swift.infocache'] (cache_key )
418
+ where info about accounts, containers, and objects is cached
431
419
432
420
:param account: The name of the account
433
421
:param container: The name of the container (or None if account)
434
- :returns: a tuple of (cache_key, env_key)
422
+ :param obj: The name of the object (or None if account or container)
423
+ :returns: a string cache_key
435
424
"""
436
425
437
- if container :
426
+ if obj :
427
+ if not (account and container ):
428
+ raise ValueError ('Object cache key requires account and container' )
429
+ cache_key = 'object/%s/%s/%s' % (account , container , obj )
430
+ elif container :
431
+ if not account :
432
+ raise ValueError ('Container cache key requires account' )
438
433
cache_key = 'container/%s/%s' % (account , container )
439
434
else :
440
435
cache_key = 'account/%s' % account
441
436
# Use a unique environment cache key per account and one container.
442
437
# This allows caching both account and container and ensures that when we
443
438
# copy this env to form a new request, it won't accidentally reuse the
444
439
# old container or account info
445
- env_key = 'swift.%s' % cache_key
446
- return cache_key , env_key
447
-
448
-
449
- def get_object_env_key (account , container , obj ):
450
- """
451
- Get the keys for env (env_key) where info about object is cached
452
-
453
- :param account: The name of the account
454
- :param container: The name of the container
455
- :param obj: The name of the object
456
- :returns: a string env_key
457
- """
458
- env_key = 'swift.object/%s/%s/%s' % (account ,
459
- container , obj )
460
- return env_key
440
+ return cache_key
461
441
462
442
463
443
def set_info_cache (app , env , account , container , resp ):
@@ -483,7 +463,7 @@ def set_info_cache(app, env, account, container, resp):
483
463
cache_time = int (resp .headers .get (
484
464
'X-Backend-Recheck-Account-Existence' ,
485
465
DEFAULT_RECHECK_ACCOUNT_EXISTENCE ))
486
- cache_key , env_key = _get_cache_key (account , container )
466
+ cache_key = get_cache_key (account , container )
487
467
488
468
if resp :
489
469
if resp .status_int in (HTTP_NOT_FOUND , HTTP_GONE ):
@@ -494,7 +474,7 @@ def set_info_cache(app, env, account, container, resp):
494
474
# Next actually set both memcache and the env cache
495
475
memcache = getattr (app , 'memcache' , None ) or env .get ('swift.cache' )
496
476
if not cache_time :
497
- infocache .pop (env_key , None )
477
+ infocache .pop (cache_key , None )
498
478
if memcache :
499
479
memcache .delete (cache_key )
500
480
return
@@ -505,7 +485,7 @@ def set_info_cache(app, env, account, container, resp):
505
485
info = headers_to_account_info (resp .headers , resp .status_int )
506
486
if memcache :
507
487
memcache .set (cache_key , info , time = cache_time )
508
- infocache [env_key ] = info
488
+ infocache [cache_key ] = info
509
489
return info
510
490
511
491
@@ -525,14 +505,14 @@ def set_object_info_cache(app, env, account, container, obj, resp):
525
505
:returns: the object info
526
506
"""
527
507
528
- env_key = get_object_env_key (account , container , obj )
508
+ cache_key = get_cache_key (account , container , obj )
529
509
530
510
if 'swift.infocache' in env and not resp :
531
- env ['swift.infocache' ].pop (env_key , None )
511
+ env ['swift.infocache' ].pop (cache_key , None )
532
512
return
533
513
534
514
info = headers_to_object_info (resp .headers , resp .status_int )
535
- env .setdefault ('swift.infocache' , {})[env_key ] = info
515
+ env .setdefault ('swift.infocache' , {})[cache_key ] = info
536
516
return info
537
517
538
518
@@ -559,9 +539,9 @@ def _get_info_from_infocache(env, account, container=None):
559
539
560
540
:returns: a dictionary of cached info on cache hit, None on miss
561
541
"""
562
- _junk , env_key = _get_cache_key (account , container )
563
- if 'swift.infocache' in env and env_key in env ['swift.infocache' ]:
564
- return env ['swift.infocache' ][env_key ]
542
+ cache_key = get_cache_key (account , container )
543
+ if 'swift.infocache' in env and cache_key in env ['swift.infocache' ]:
544
+ return env ['swift.infocache' ][cache_key ]
565
545
return None
566
546
567
547
@@ -577,7 +557,7 @@ def _get_info_from_memcache(app, env, account, container=None):
577
557
:returns: a dictionary of cached info on cache hit, None on miss. Also
578
558
returns None if memcache is not in use.
579
559
"""
580
- cache_key , env_key = _get_cache_key (account , container )
560
+ cache_key = get_cache_key (account , container )
581
561
memcache = getattr (app , 'memcache' , None ) or env .get ('swift.cache' )
582
562
if memcache :
583
563
info = memcache .get (cache_key )
@@ -589,7 +569,7 @@ def _get_info_from_memcache(app, env, account, container=None):
589
569
for subkey , value in info [key ].items ():
590
570
if isinstance (value , six .text_type ):
591
571
info [key ][subkey ] = value .encode ("utf-8" )
592
- env .setdefault ('swift.infocache' , {})[env_key ] = info
572
+ env .setdefault ('swift.infocache' , {})[cache_key ] = info
593
573
return info
594
574
return None
595
575
@@ -680,8 +660,8 @@ def _get_object_info(app, env, account, container, obj, swift_source=None):
680
660
:param obj: The unquoted name of the object
681
661
:returns: the cached info or None if cannot be retrieved
682
662
"""
683
- env_key = get_object_env_key (account , container , obj )
684
- info = env .get ('swift.infocache' , {}).get (env_key )
663
+ cache_key = get_cache_key (account , container , obj )
664
+ info = env .get ('swift.infocache' , {}).get (cache_key )
685
665
if info :
686
666
return info
687
667
# Not in cache, let's try the object servers
0 commit comments