Skip to content

Commit

Permalink
Merge pull request #4128 from benjaoming/cache-settings
Browse files Browse the repository at this point in the history
Cache settings #4125
  • Loading branch information
rtibbles committed Jul 29, 2015
2 parents e6718c2 + b638558 commit b8692f5
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 51 deletions.
7 changes: 7 additions & 0 deletions docs/usermanual/userman_admin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,13 @@ User restrictions
Disables user sign ups.


..note:: KA Lite uses caching of web pages, if you change ``LOCKDOWN`` or
``DISABLE_SELF_ADMIN``, you need to flush the cache. To do that, run
the following management command::
kalite manage cache clearweb


Online Synchronization
^^^^^^^^^^^^^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion kalite/caching/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from django.core.urlresolvers import reverse
from django.test.client import Client

from fle_utils.internet.webcache import *
from fle_utils.internet.webcache import get_web_cache, has_cache_key, expire_page, caching_is_enabled, invalidate_web_cache
from kalite import i18n, topic_tools
from kalite.topic_tools.settings import DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP

Expand Down
3 changes: 0 additions & 3 deletions kalite/distributed/management/commands/initialize_kalite.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ def setup_server_if_needed(self):

def reinitialize_server(self):
"""Reset the server state."""
logging.info("Invalidating the web cache.")
from fle_utils.internet.webcache import invalidate_web_cache
invalidate_web_cache()

# Next, call videoscan.
logging.info("Running videoscan.")
Expand Down
2 changes: 0 additions & 2 deletions kalite/distributed/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

from fle_utils.internet.classes import JsonResponseMessageError
from fle_utils.internet.functions import get_ip_addresses, set_query_params
from fle_utils.internet.webcache import backend_cache_page
from fle_utils.django_utils.paginate import paginate_data
from kalite import topic_tools
from kalite.shared.decorators.auth import require_admin
from securesync.api_client import BaseClient
Expand Down
2 changes: 0 additions & 2 deletions kalite/main/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@

from fle_utils.internet.decorators import api_handle_error_with_json
from fle_utils.internet.classes import JsonResponse, JsonResponseMessageError
from fle_utils.internet.webcache import backend_cache_page

from kalite.topic_tools import get_topic_tree
from kalite.topic_tools.content_recommendation import get_resume_recommendations, get_next_recommendations, get_explore_recommendations
from kalite.facility.models import FacilityUser

@api_handle_error_with_json
@backend_cache_page
def topic_tree(request, channel):
parent = request.GET.get("parent")
return JsonResponse(get_topic_tree(channel=channel, language=request.language, parent=parent))
Expand Down
9 changes: 7 additions & 2 deletions kalite/project/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@
'debug_toolbar.panels.logging.LoggingPanel',
'debug_toolbar.panels.redirects.RedirectsPanel', # This belongs to DISABLE_PANELS by default
)

DEBUG_TOOLBAR_CONFIG = {
'ENABLE_STACKTRACES': True,
}
# Debug toolbar must be set in conjunction with CACHE_TIME=0
CACHE_TIME = 0

CACHES["default"] = {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
'TIMEOUT': 24 * 60 * 60 # = 24 hours
}
3 changes: 0 additions & 3 deletions kalite/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ def package_selected(package_name):

if package_selected("UserRestricted"):
LOG.info("UserRestricted package selected.")

if CACHE_TIME != 0 and not hasattr(local_settings, KEY_PREFIX):
KEY_PREFIX += "|restricted" # this option changes templates
DISABLE_SELF_ADMIN = True # hard-code facility app setting.


Expand Down
49 changes: 16 additions & 33 deletions kalite/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,48 +438,31 @@
_100_years = 100 * 365 * 24 * 60 * 60
_max_cache_time = min(_100_years, sys.maxint - time.time() - _5_years)
CACHE_TIME = getattr(local_settings, "CACHE_TIME", _max_cache_time)
CACHE_NAME = getattr(local_settings, "CACHE_NAME", None) # without a cache defined, None is fine

# Sessions use the default cache, and we want a local memory cache for that.
CACHE_LOCATION = os.path.realpath(getattr(
local_settings,
"CACHE_LOCATION",
os.path.join(
USER_DATA_ROOT,
'cache',
)
))

CACHES = {
"default": {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}
}

# Cache is activated in every case,
# EXCEPT: if CACHE_TIME=0
if CACHE_TIME != 0: # None can mean infinite caching to some functions
# When we change versions, cache changes, too
KEY_PREFIX = ".".join(version.VERSION)

# File-based cache
install_location_hash = hashlib.sha1(".".join(version.VERSION)).hexdigest()
username = getpass.getuser() or "unknown_user"
cache_dir_name = "kalite_web_cache_%s" % (username)
CACHE_LOCATION = os.path.realpath(getattr(local_settings, "CACHE_LOCATION", os.path.join(tempfile.gettempdir(), cache_dir_name, install_location_hash))) + "/"
CACHES["file_based_cache"] = {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': CACHE_LOCATION, # this is kind of OS-specific, so dangerous.
'TIMEOUT': CACHE_TIME, # should be consistent
'LOCATION': CACHE_LOCATION, # this is kind of OS-specific, so dangerous.
'TIMEOUT': CACHE_TIME, # should be consistent
'OPTIONS': {
'MAX_ENTRIES': getattr(local_settings, "CACHE_MAX_ENTRIES", 5*2000) #2000 entries=~10,000 files
'MAX_ENTRIES': getattr(local_settings, "CACHE_MAX_ENTRIES", 5 * 2000) # 2000 entries=~10,000 files
},
}
}

# Memory-based cache
CACHES["mem_cache"] = {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
'TIMEOUT': CACHE_TIME, # should be consistent
'OPTIONS': {
'MAX_ENTRIES': getattr(local_settings, "CACHE_MAX_ENTRIES", 5*2000) #2000 entries=~10,000 files
},
}

# The chosen cache
CACHE_NAME = getattr(local_settings, "CACHE_NAME", "file_based_cache")

# Prefix the cache with the version string so we don't experience problems with
# updates
KEY_PREFIX = version.VERSION

# Separate session caching from file caching.
SESSION_ENGINE = getattr(
Expand Down
8 changes: 3 additions & 5 deletions python-packages/fle_utils/internet/webcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
from django.core.cache.backends.filebased import FileBasedCache
from django.core.cache.backends.locmem import LocMemCache
from django.core.urlresolvers import reverse
from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from django.http import HttpRequest
from django.test.client import Client
from django.utils import translation
from django.utils.cache import get_cache_key as django_get_cache_key, get_cache, _generate_cache_key
from django.utils.cache import get_cache_key as django_get_cache_key, get_cache
from django.views.decorators.cache import cache_control
from django.views.decorators.cache import cache_page
from django.views.decorators.http import condition
Expand Down Expand Up @@ -66,7 +64,7 @@ def backend_cache_page(handler, cache_time=None, cache_name=None):
cache_time = settings.CACHE_TIME

if not cache_name:
cache_name = settings.CACHE_NAME
cache_name = "default"

if caching_is_enabled():
@condition(last_modified_func=partial(calc_last_modified, cache_name=cache_name))
Expand All @@ -89,7 +87,7 @@ def caching_is_enabled():
return settings.CACHE_TIME != 0

def get_web_cache():
return get_cache(settings.CACHE_NAME) if caching_is_enabled() else None
return get_cache('default') if caching_is_enabled() else None


def get_cache_key(path=None, url_name=None, cache=None, failure_ok=False):
Expand Down

0 comments on commit b8692f5

Please sign in to comment.