
    Bh@                     n    d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 d Z
d ZddZdd	Zd
 Zd Zd Zy)a~  
This module manages placeholder caching. We use a cache-versioning strategy
in which each (placeholder x lang x site_id) manages its own version. The
actual cache includes additional keys appropriate for the placeholders
get_vary_cache_on().

Invalidation of a placeholder's cache simply increments the version number for
the (placeholder x lang) pair, which renders any cache entries for that
placeholder under that version inaccessible. Those cache entries will simply
expire and will be purged according to the policy of the cache backend in-use.

The cache entries themselves may include additional sub-keys, according to the
list of VARY header-names as returned by placeholder.get_vary_cache_on() and
the current HTTPRequest object.

The vary-on header-names are also stored with the version. This enables us to
check for cache hits without re-computing placeholder.get_vary_cache_on().
    N)now)get_cms_setting)get_header_nameget_timezone_namec                     t        d      }| d| j                   dt        |       d| }t        |      dkD  rCdj	                  |t        j                  |j                  d            j                               }|S )	u[  
    Returns the version key for the given «placeholder», «lang» and «site_id».

    Invalidating this (via clear_placeholder_cache by replacing the stored
    value with a new value) will effectively make all "sub-caches" relating to
    this (placeholder x lang) inaccessible. Sub-caches include caches per TZ
    and per VARY header.
    CACHE_PREFIXz|placeholder_cache_version|id:|lang:|site:   {prefix}|{hash}utf-8prefixhash)	r   pkstrlenformathashlibsha1encode	hexdigest)placeholderlangsite_idr   keys        E/home/dcms/DCMS/lib/python3.12/site-packages/cms/cache/placeholder.py"_get_placeholder_cache_version_keyr      s     ^,FH2;>>2B&TSYZaYb
cC 3x#~&&cjj12<<> ' 
 J    c                     ddl m} t        | ||      }|j                  |      }|r	|\  }}||fS t	        t        j
                         dz        }g }t        | ||||       ||fS )u   
    Gets the (placeholder x lang)'s current version and vary-on header-names
    list, if present, otherwise resets to («timestamp», []).
    r   cache@B )django.core.cacher"   r   getinttime_set_placeholder_cache_version)r   r   r   r"   r   cachedversionvary_on_lists           r   _get_placeholder_cache_versionr,   5   ss    
 (
,[$
HCYYs^F &
 L   diikG+,&{D'7LYL  r   c                     ddl m} t        | ||      }|r|dk  r t        t	        j                         dz        }|g }|j                  |||f|       y)zP
    Sets the (placeholder x lang)'s version and vary-on header-names list.
    r   r!      r#   N)r$   r"   r   r&   r'   set)r   r   r   r*   r+   durationr"   r   s           r   r(   r(   G   sS     (
,[$
HCgkdiikG+,	IIcG\*H5r   c                 l   t        d      }t        | ||      \  }}t               }| d| j                   d| d| d| d| }	|s;| j	                  |      }| j                  |t                     }
t        | |||||
       g }|D ]A  }|j                  j                  t        |            xs d}|j                  |dz   |z          C |	}|r|d	d	j                  |      z   z  }t        |      d
kD  rCdj                  |t        j                   |j#                  d            j%                               }|S )u  
    Returns the fully-addressed cache key for the given placeholder and
    the request.

    The kwarg «soft» should be set to True if getting the cache key to then
    read from the cache. If instead the key retrieval is to support a cache
    write, let «soft» be False.
    r   z|render_placeholder|id:r	   r
   z|tz:z|v:_:|r   r   r   r   )r   r,   r   r   get_vary_cache_onget_cache_expirationr   r(   METAr%   r   appendjoinr   r   r   r   r   r   )r   r   r   requestsoftr   r*   r+   tzmain_keyr0   sub_key_listr   value	cache_keys                  r   _get_placeholder_cache_keyrA   X   sT    ^,F:;gVG\		B00@tfFSZR[[_`b_ccfgnfopH #44W=33GSUC&{D'7LZbcL /  !56=#C#I-./ IS388L111	 9~%,,i..w78BBD - 
	
 r   c                     ddl m} t        | |||      }t        t	        d      d   | j                  |t                           }|j                  |||       t        | ||      \  }}	t        | ||||	|       y)zM
    Sets the (correct) placeholder cache with the rendered placeholder.
    r   r!   CACHE_DURATIONScontent)r0   N)
r$   r"   rA   minr   r6   r   r/   r,   r(   )
r   r   r   rD   r:   r"   r   r0   r*   r+   s
             r   set_placeholder_cacherF      sv     (
$[$
IC?#45i@+BbBbcjlolqBrsH	IIc7H%:;gVG\";gw_ghr   c                 T    ddl m} t        | |||d      }|j                  |      }|S )z[
    Returns the placeholder from cache respecting the placeholder's
    VARY headers.
    r   r!   T)r;   )r$   r"   rA   r%   )r   r   r   r:   r"   r   rD   s          r   get_placeholder_cacherH      s+    
 (
$[$t
TCiinGNr   c                 b    t        t        j                         dz        }t        | |||g        y)z
    Invalidates all existing cache entries for (placeholder x lang x site_id).
    We don't need to re-store the vary_on_list, because the cache is now
    effectively empty.
    r#   N)r&   r'   r(   )r   r   r   r*   s       r   clear_placeholder_cacherJ      s)     $))+'(G";gwKr   )NN)F)__doc__r   r'   django.utils.timezoner   cms.utils.confr   cms.utils.helpersr   r   r   r,   r(   rA   rF   rH   rJ    r   r   <module>rP      sA   $   % * @2!$6".bi	Lr   