Commit d13e7da2 by David Ormsbee

Move DiscussionModule changes from other working branch

parent 550c1913
...@@ -77,8 +77,9 @@ class RequestPlusRemoteCache(BaseCache): ...@@ -77,8 +77,9 @@ class RequestPlusRemoteCache(BaseCache):
""" """
This Django cache backend implements two layers of caching. This Django cache backend implements two layers of caching.
Layer 1 is a threadlocal dictionary that is tied to the life of a given The first layer is a threadlocal dictionary that is tied to the life of a
request. given request. The second layer is another named Django cache -- e.g. the
"default" entry in settings.CACHES, typically backed by memcached.
Some baseline rules: Some baseline rules:
...@@ -90,8 +91,12 @@ class RequestPlusRemoteCache(BaseCache): ...@@ -90,8 +91,12 @@ class RequestPlusRemoteCache(BaseCache):
2. Timeouts are ignored for the purposes of the in-memory request cache, but 2. Timeouts are ignored for the purposes of the in-memory request cache, but
do apply to the backing remote cache. One consequence of this is that do apply to the backing remote cache. One consequence of this is that
sending an explicit timeout of 0 in `set` or `add` will cause that item sending an explicit timeout of 0 in `set` or `add` will cause that item
to only be cached across the duration of the request and never make it to only be cached across the duration of the request and will not cause
out to the backing remote cache. a write to the remote cache.
3. If you're in a situation where key generation performance is actually a
concern (many thousands of lookups), then just use the request cache
directly instead of this hybrid.
""" """
def __init__(self, name, params): def __init__(self, name, params):
try: try:
......
...@@ -73,7 +73,6 @@ class DiscussionModule(DiscussionFields, XModule): ...@@ -73,7 +73,6 @@ class DiscussionModule(DiscussionFields, XModule):
js_module_name = "InlineDiscussion" js_module_name = "InlineDiscussion"
def get_html(self): def get_html(self):
course = self.get_course()
user = None user = None
user_service = self.runtime.service(self, 'user') user_service = self.runtime.service(self, 'user')
if user_service: if user_service:
...@@ -90,7 +89,6 @@ class DiscussionModule(DiscussionFields, XModule): ...@@ -90,7 +89,6 @@ class DiscussionModule(DiscussionFields, XModule):
can_create_thread = False can_create_thread = False
context = { context = {
'discussion_id': self.discussion_id, 'discussion_id': self.discussion_id,
'course': course,
'can_create_comment': json.dumps(can_create_comment), 'can_create_comment': json.dumps(can_create_comment),
'can_create_subcomment': json.dumps(can_create_subcomment), 'can_create_subcomment': json.dumps(can_create_subcomment),
'can_create_thread': can_create_thread, 'can_create_thread': can_create_thread,
...@@ -110,10 +108,14 @@ class DiscussionModule(DiscussionFields, XModule): ...@@ -110,10 +108,14 @@ class DiscussionModule(DiscussionFields, XModule):
cache_key = (user, course_id, permission) cache_key = (user, course_id, permission)
cached_answer = cache.get(cache_key) cached_answer = cache.get(cache_key)
if cached_answer is not None: if cached_answer is not None:
print "Cache hit: {}, {}".format(cache_key, cached_answer)
return cached_answer return cached_answer
has_perm = any(role.has_permission(permission) for role in user.roles.filter(course_id=course_id)) has_perm = any(role.has_permission(permission) for role in user.roles.filter(course_id=course_id))
cache.set(cache_key, has_perm, 0)
# Only cache for the duration of this single request
cache.set(cache_key, has_perm, timeout=0)
print "Cache set {}, {}".format(cache_key, has_perm)
return has_perm return has_perm
......
...@@ -201,41 +201,11 @@ class CacheService(object): ...@@ -201,41 +201,11 @@ class CacheService(object):
""" """
""" """
def __init__(self, req_cache_root, django_cache=None): def __init__(self, django_cache):
""" """
Create a CacheService that can generate named Caches for XBlocks. Create a CacheService that can generate named Caches for XBlocks.
`req_cache_root` should be a `dict`-like object, and will be where we
store the in-memory cached version of various objects during a single
request. Each named cache that is returned by `get_cache()` will create
a new entry here. So for example, if you did the following from inside
an XBlock:
cache_service = self.runtime.service(self, 'cache')
perm_cache = cache_service.get_cache("discussion.permissions")
tmpl_cache = cache_service.get_cache("discussion.templates")
Then `req_cache_root` would look like:
{
"discussion.permissions": {},
"discussion.templates": {}
}
`django_cache` is the explicit Django low level cache object we want to
use, in case there's a specific named cache that is preferred. By
default, it'll just use django.core.cache.cache.
""" """
self._django_cache = django_cache
# Create an entry in the request_cache where we will put all
# CacheService generated cache data. We'll further namespace with our
# own get_cache() method. So:
#
# request_cache = {
#
# }
self._local_cache_root = local_cache_root
self._django_cache = django_cache or caches['request']
def get_cache(self, name): def get_cache(self, name):
""" """
...@@ -245,10 +215,7 @@ class CacheService(object): ...@@ -245,10 +215,7 @@ class CacheService(object):
malicious behavior. The `name` parameter should include your XBlock's malicious behavior. The `name` parameter should include your XBlock's
tag as a prefix parameter. tag as a prefix parameter.
""" """
if name not in self._request_cache: return Cache(name, self._django_cache)
self._request_cache[name] = {}
return Cache(name, self._request_cache, self._django_cache)
class Cache(object): class Cache(object):
...@@ -263,28 +230,41 @@ class Cache(object): ...@@ -263,28 +230,41 @@ class Cache(object):
cache.set(key, value, timeout) cache.set(key, value, timeout)
""" """
def __init__(self, name, django_cache):
def __init__(self, name, cache_dict, django_cache): self._name = name
self._cache_dict = cache_dict
self._django_cache = django_cache self._django_cache = django_cache
def get(self, key): def _namespaced_key(self, key):
pass return (self._name, key)
def get_many(self, keys): def get(self, key, default=None, version=None):
pass return self._django_cache.get(
self._namespaced_key(key), default=default, version=version
)
def set(self, key, value, timeout, version=1): def get_many(self, keys, version=None):
""" namespaced_keys = [self._namespaced_key(key) for key in keys]
Set key -> value mapping in cache. return self._django_cache.get_many(namespaced_keys, version=version)
`key` should be hashable def set(self, key, value, timeout=0, version=None):
self._django_cache.set(
self._namespaced_key(key), value, timeout=timeout, version=version
)
""" def set_many(self, kv_dict, timeout=0, version=None):
self._cache_dict[key] = value namedspaced_dict = {
self._namespaced_key(key): val for key, val in kv_dict.items()
}
self._django_cache.set_many(
namedspaced_dict, timeout=timeout, version=version
)
def set_many(self, kv_dict, timeout): def delete(self, key, version=None):
pass self._django_cache.delete(self._namespaced_key(key), version=version)
def delete_many(self, keys, version=None):
namespaced_keys = [self._namespaced_key(key) for key in keys]
self._django_cache.delete_many(namespaced_keys, version=version)
class LmsModuleSystem(ModuleSystem): # pylint: disable=abstract-method class LmsModuleSystem(ModuleSystem): # pylint: disable=abstract-method
...@@ -308,8 +288,7 @@ class LmsModuleSystem(ModuleSystem): # pylint: disable=abstract-method ...@@ -308,8 +288,7 @@ class LmsModuleSystem(ModuleSystem): # pylint: disable=abstract-method
services['user_tags'] = UserTagsService(self) services['user_tags'] = UserTagsService(self)
if badges_enabled(): if badges_enabled():
services['badging'] = BadgingService(course_id=kwargs.get('course_id'), modulestore=store) services['badging'] = BadgingService(course_id=kwargs.get('course_id'), modulestore=store)
services['cache'] = CacheService(request_cache.get_cache("xblock_cache_service")) services['cache'] = CacheService(caches['request'])
self.request_token = kwargs.pop('request_token', None) self.request_token = kwargs.pop('request_token', None)
super(LmsModuleSystem, self).__init__(**kwargs) super(LmsModuleSystem, self).__init__(**kwargs)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment