from django.utils import timezone
from django.conf import settings
from django.contrib.sites.models import get_current_site

from .models import Channel


def channel_context(request):
    """ Channel context processors
    """
    site = get_current_site(request)
    opps_menu = Channel.objects.filter(site=site,
                                       date_available__lte=timezone.now(),
                                       published=True,
                                       show_in_menu=True).order_by('order')

    return {'opps_menu': opps_menu,
            'opps_channel_conf_all': settings.OPPS_CHANNEL_CONF,
            'site': site}

from django.utils import timezone
from django.conf import settings
from django.contrib.sites.models import get_current_site
from django.core.cache import cache

from .models import Channel


def channel_context(request):
    """ Channel context processors
    """
    site = get_current_site(request)
    opps_menu = cache.get('opps_menu')
    if not opps_menu:
        opps_menu = [channel for channel in Channel.objects.filter(
            site=site,
            date_available__lte=timezone.now(),
            published=True,
            show_in_menu=True).distinct().order_by('order')]
        cache.set('opps_menu', opps_menu, settings.OPPS_CACHE_EXPIRE)

    return {'opps_menu': opps_menu,
            'opps_channel_conf_all': settings.OPPS_CHANNEL_CONF,
            'site': site}

Add cache in opps menu list via context processors
Add cache in opps menu list via context processors

  from django.utils import timezone
  from django.conf import settings
  from django.contrib.sites.models import get_current_site
+ from django.core.cache import cache
  
  from .models import Channel
  
  
  def channel_context(request):
      """ Channel context processors
      """
      site = get_current_site(request)
-     opps_menu = Channel.objects.filter(site=site,
+     opps_menu = cache.get('opps_menu')
+     if not opps_menu:
+         opps_menu = [channel for channel in Channel.objects.filter(
+             site=site,
-                                        date_available__lte=timezone.now(),
+             date_available__lte=timezone.now(),
-                                        published=True,
-                                        show_in_menu=True).order_by('order')
+             published=True,
+             show_in_menu=True).distinct().order_by('order')]
+         cache.set('opps_menu', opps_menu, settings.OPPS_CACHE_EXPIRE)
  
      return {'opps_menu': opps_menu,
              'opps_channel_conf_all': settings.OPPS_CHANNEL_CONF,
              'site': site}
  
Add cache in opps menu list via context processors
## Code Before:
from django.utils import timezone
from django.conf import settings
from django.contrib.sites.models import get_current_site

from .models import Channel


def channel_context(request):
    """ Channel context processors
    """
    site = get_current_site(request)
    opps_menu = Channel.objects.filter(site=site,
                                       date_available__lte=timezone.now(),
                                       published=True,
                                       show_in_menu=True).order_by('order')

    return {'opps_menu': opps_menu,
            'opps_channel_conf_all': settings.OPPS_CHANNEL_CONF,
            'site': site}

## Instruction:
Add cache in opps menu list via context processors
## Code After:
from django.utils import timezone
from django.conf import settings
from django.contrib.sites.models import get_current_site
from django.core.cache import cache

from .models import Channel


def channel_context(request):
    """ Channel context processors
    """
    site = get_current_site(request)
    opps_menu = cache.get('opps_menu')
    if not opps_menu:
        opps_menu = [channel for channel in Channel.objects.filter(
            site=site,
            date_available__lte=timezone.now(),
            published=True,
            show_in_menu=True).distinct().order_by('order')]
        cache.set('opps_menu', opps_menu, settings.OPPS_CACHE_EXPIRE)

    return {'opps_menu': opps_menu,
            'opps_channel_conf_all': settings.OPPS_CHANNEL_CONF,
            'site': site}
