<s> from __future__ import absolute_import <EOL> from typing import Any <EOL> from django . conf import settings <EOL> from zerver . models import Message , UserProfile , Stream , get_stream_cache_key , Recipient , get_recipient_cache_key , Client , get_client_cache_key , Huddle , huddle_hash_cache_key <EOL> from zerver . lib . cache import cache_with_key , cache_set , message_cache_key , user_profile_by_email_cache_key , user_profile_by_id_cache_key , get_remote_cache_time , get_remote_cache_requests , cache_set_many <EOL> from django . utils . importlib import import_module <EOL> from django . contrib . sessions . models import Session <EOL> import logging <EOL> from django . db . models import Q <EOL> MESSAGE_CACHE_SIZE = <NUM_LIT> <EOL> def cache_save_message ( message ) : <EOL> cache_set ( message_cache_key ( message . id ) , message , timeout = <NUM_LIT> * <NUM_LIT> ) <EOL> @ cache_with_key ( message_cache_key , timeout = <NUM_LIT> * <NUM_LIT> ) <EOL> def cache_get_message ( message_id ) : <EOL> return Message . objects . select_related ( ) . get ( id = message_id ) <EOL> def message_fetch_objects ( ) : <EOL> try : <EOL> max_id = Message . objects . only ( '<STR_LIT:id>' ) . order_by ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . id <EOL> except IndexError : <EOL> return [ ] <EOL> return Message . objects . select_related ( ) . filter ( ~ Q ( sender__email = '<STR_LIT>' ) , <EOL> id__gt = max_id - MESSAGE_CACHE_SIZE ) <EOL> def message_cache_items ( items_for_remote_cache , message ) : <EOL> items_for_remote_cache [ message_cache_key ( message . id ) ] = ( message , ) <EOL> def user_cache_items ( items_for_remote_cache , user_profile ) : <EOL> items_for_remote_cache [ user_profile_by_email_cache_key ( user_profile . email ) ] = ( user_profile , ) <EOL> items_for_remote_cache [ user_profile_by_id_cache_key ( user_profile . id ) ] = ( user_profile , ) <EOL> def stream_cache_items ( items_for_remote_cache , stream ) : <EOL> items_for_remote_cache [ get_stream_cache_key ( stream . name , stream . realm_id ) ] = ( stream , ) <EOL> def client_cache_items ( items_for_remote_cache , client ) : <EOL> items_for_remote_cache [ get_client_cache_key ( client . name ) ] = ( client , ) <EOL> def huddle_cache_items ( items_for_remote_cache , huddle ) : <EOL> items_for_remote_cache [ huddle_hash_cache_key ( huddle . huddle_hash ) ] = ( huddle , ) <EOL> def recipient_cache_items ( items_for_remote_cache , recipient ) : <EOL> items_for_remote_cache [ get_recipient_cache_key ( recipient . type , recipient . type_id ) ] = ( recipient , ) <EOL> session_engine = import_module ( settings . SESSION_ENGINE ) <EOL> def session_cache_items ( items_for_remote_cache , session ) : <EOL> store = session_engine . SessionStore ( session_key = session . session_key ) <EOL> items_for_remote_cache [ store . cache_key ] = store . decode ( session . session_data ) <EOL> cache_fillers = { <EOL> '<STR_LIT:user>' : ( lambda : UserProfile . objects . select_related ( ) . all ( ) , user_cache_items , <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( lambda : Client . objects . select_related ( ) . all ( ) , client_cache_items , <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( lambda : Recipient . objects . select_related ( ) . all ( ) , recipient_cache_items , <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( lambda : Stream . objects . select_related ( ) . all ( ) , stream_cache_items , <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> , <NUM_LIT> ) , <EOL> '<STR_LIT:message>' : ( message_fetch_objects , message_cache_items , <NUM_LIT> * <NUM_LIT> , <NUM_LIT:1000> ) , <EOL> '<STR_LIT>' : ( lambda : Huddle . objects . select_related ( ) . all ( ) , huddle_cache_items , <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> , <NUM_LIT> ) , <EOL> '<STR_LIT>' : ( lambda : Session . objects . all ( ) , session_cache_items , <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> , <NUM_LIT> ) , <EOL> } <EOL> def fill_remote_cache ( cache ) : <EOL> remote_cache_time_start = get_remote_cache_time ( ) <EOL> remote_cache_requests_start = get_remote_cache_requests ( ) <EOL> items_for_remote_cache = { } <EOL> ( objects , items_filler , timeout , batch_size ) = cache_fillers [ cache ] <EOL> count = <NUM_LIT:0> <EOL> for obj in objects ( ) : <EOL> items_filler ( items_for_remote_cache , obj ) <EOL> count += <NUM_LIT:1> <EOL> if ( count % batch_size == <NUM_LIT:0> ) : <EOL> cache_set_many ( items_for_remote_cache , timeout = <NUM_LIT> * <NUM_LIT> ) <EOL> items_for_remote_cache = { } <EOL> cache_set_many ( items_for_remote_cache , timeout = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> ) <EOL> logging . info ( "<STR_LIT>" % ( cache , get_remote_cache_requests ( ) - remote_cache_requests_start , <EOL> round ( get_remote_cache_time ( ) - remote_cache_time_start , <NUM_LIT:2> ) ) ) </s>
