Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
2c7975f8
Commit
2c7975f8
authored
Oct 06, 2016
by
Nimisha Asthagiri
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move cache_toolbox from common to openedx/core.
parent
4bb88109
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
69 additions
and
28 deletions
+69
-28
cms/envs/common.py
+1
-1
lms/envs/common.py
+1
-1
openedx/core/djangoapps/cache_toolbox/COPYING
+0
-0
openedx/core/djangoapps/cache_toolbox/README.rst
+0
-0
openedx/core/djangoapps/cache_toolbox/__init__.py
+0
-0
openedx/core/djangoapps/cache_toolbox/app_settings.py
+4
-0
openedx/core/djangoapps/cache_toolbox/core.py
+9
-13
openedx/core/djangoapps/cache_toolbox/middleware.py
+5
-2
openedx/core/djangoapps/cache_toolbox/model.py
+12
-2
openedx/core/djangoapps/cache_toolbox/relation.py
+21
-7
openedx/core/djangoapps/cache_toolbox/templatetags/__init__.py
+0
-0
openedx/core/djangoapps/cache_toolbox/templatetags/cache_toolbox.py
+16
-2
openedx/core/djangoapps/cache_toolbox/tests/__init__.py
+0
-0
openedx/core/djangoapps/cache_toolbox/tests/test_middleware.py
+0
-0
No files found.
cms/envs/common.py
View file @
2c7975f8
...
...
@@ -347,7 +347,7 @@ MIDDLEWARE_CLASSES = (
'method_override.middleware.MethodOverrideMiddleware'
,
# Instead of AuthenticationMiddleware, we use a cache-backed version
'cache_toolbox.middleware.CacheBackedAuthenticationMiddleware'
,
'
openedx.core.djangoapps.
cache_toolbox.middleware.CacheBackedAuthenticationMiddleware'
,
# Enable SessionAuthenticationMiddleware in order to invalidate
# user sessions after a password change.
'django.contrib.auth.middleware.SessionAuthenticationMiddleware'
,
...
...
lms/envs/common.py
View file @
2c7975f8
...
...
@@ -1098,7 +1098,7 @@ MIDDLEWARE_CLASSES = (
# Instead of AuthenticationMiddleware, we use a cached backed version
#'django.contrib.auth.middleware.AuthenticationMiddleware',
'cache_toolbox.middleware.CacheBackedAuthenticationMiddleware'
,
'
openedx.core.djangoapps.
cache_toolbox.middleware.CacheBackedAuthenticationMiddleware'
,
# Enable SessionAuthenticationMiddleware in order to invalidate
# user sessions after a password change.
'django.contrib.auth.middleware.SessionAuthenticationMiddleware'
,
...
...
common
/djangoapps/cache_toolbox/COPYING
→
openedx/core
/djangoapps/cache_toolbox/COPYING
View file @
2c7975f8
File moved
common
/djangoapps/cache_toolbox/README.rst
→
openedx/core
/djangoapps/cache_toolbox/README.rst
View file @
2c7975f8
File moved
common
/djangoapps/cache_toolbox/__init__.py
→
openedx/core
/djangoapps/cache_toolbox/__init__.py
View file @
2c7975f8
File moved
common
/djangoapps/cache_toolbox/app_settings.py
→
openedx/core
/djangoapps/cache_toolbox/app_settings.py
View file @
2c7975f8
"""
Settings for cache_toolbox.
"""
from
django.conf
import
settings
# Default cache timeout
...
...
common
/djangoapps/cache_toolbox/core.py
→
openedx/core
/djangoapps/cache_toolbox/core.py
View file @
2c7975f8
...
...
@@ -34,40 +34,36 @@ def get_instance(model, instance_or_pk, timeout=None, using=None):
True
"""
p
k
=
getattr
(
instance_or_pk
,
'pk'
,
instance_or_pk
)
p
rimary_key
=
getattr
(
instance_or_pk
,
'pk'
,
instance_or_pk
)
key
=
instance_key
(
model
,
instance_or_pk
)
data
=
cache
.
get
(
key
)
if
data
is
not
None
:
try
:
# Try and construct instance from dictionary
instance
=
model
(
pk
=
p
k
,
**
data
)
instance
=
model
(
pk
=
p
rimary_key
,
**
data
)
# Ensure instance knows that it already exists in the database,
# otherwise we will fail any uniqueness checks when saving the
# instance.
instance
.
_state
.
adding
=
False
instance
.
_state
.
adding
=
False
# pylint: disable=protected-access
# Specify database so that instance is setup correctly. We don't
# namespace cached objects by their origin database, however.
instance
.
_state
.
db
=
using
or
DEFAULT_DB_ALIAS
instance
.
_state
.
db
=
using
or
DEFAULT_DB_ALIAS
# pylint: disable=protected-access
return
instance
except
:
except
:
# pylint: disable=bare-except
# Error when deserialising - remove from the cache; we will
# fallback and return the underlying instance
cache
.
delete
(
key
)
# Use the default manager so we are never filtered by a .get_queryset()
# import logging
# log = logging.getLogger("tracking")
# log.info( str(pk) )
instance
=
model
.
_default_manager
.
using
(
using
)
.
get
(
pk
=
pk
)
instance
=
model
.
_default_manager
.
using
(
using
)
.
get
(
pk
=
primary_key
)
# pylint: disable=protected-access
data
=
{}
for
field
in
instance
.
_meta
.
fields
:
for
field
in
instance
.
_meta
.
fields
:
# pylint: disable=protected-access
# Harmless to save, but saves space in the dictionary - we already know
# the primary key when we lookup
if
field
.
primary_key
:
...
...
@@ -76,8 +72,8 @@ def get_instance(model, instance_or_pk, timeout=None, using=None):
if
field
.
get_internal_type
()
==
'FileField'
:
# Avoid problems with serializing FileFields
# by only serializing the file name
file
=
getattr
(
instance
,
field
.
attname
)
data
[
field
.
attname
]
=
file
.
name
file
_value
=
getattr
(
instance
,
field
.
attname
)
data
[
field
.
attname
]
=
file
_value
.
name
else
:
data
[
field
.
attname
]
=
getattr
(
instance
,
field
.
attname
)
...
...
common
/djangoapps/cache_toolbox/middleware.py
→
openedx/core
/djangoapps/cache_toolbox/middleware.py
View file @
2c7975f8
...
...
@@ -67,7 +67,7 @@ with::
MIDDLEWARE_CLASSES = [
...
'cache_toolbox.middleware.CacheBackedAuthenticationMiddleware',
'
openedx.core.djangoapps.
cache_toolbox.middleware.CacheBackedAuthenticationMiddleware',
...
]
...
...
@@ -93,6 +93,9 @@ log = getLogger(__name__)
class
CacheBackedAuthenticationMiddleware
(
AuthenticationMiddleware
):
"""
See documentation above.
"""
def
__init__
(
self
):
cache_model
(
User
)
...
...
@@ -110,7 +113,7 @@ class CacheBackedAuthenticationMiddleware(AuthenticationMiddleware):
# Raise an exception to fall through to the except clause below.
raise
Exception
self
.
_verify_session_auth
(
request
)
except
:
except
:
# pylint: disable=bare-except
# Fallback to constructing the User from the database.
super
(
CacheBackedAuthenticationMiddleware
,
self
)
.
process_request
(
request
)
...
...
common
/djangoapps/cache_toolbox/model.py
→
openedx/core
/djangoapps/cache_toolbox/model.py
View file @
2c7975f8
...
...
@@ -60,18 +60,28 @@ from .core import get_instance, delete_instance
def
cache_model
(
model
,
timeout
=
None
):
"""
Adds utility methods to the given model to obtain
``ForeignKey`` instances via the cache.
"""
if
hasattr
(
model
,
'get_cached'
):
# Already patched
return
def
clear_cache
(
sender
,
instance
,
*
args
,
**
kwargs
):
def
clear_cache
(
sender
,
instance
,
*
args
,
**
kwargs
):
# pylint: disable=unused-argument
"""
Clears the cache for the given instance.
"""
delete_instance
(
sender
,
instance
)
post_save
.
connect
(
clear_cache
,
sender
=
model
,
weak
=
False
)
post_delete
.
connect
(
clear_cache
,
sender
=
model
,
weak
=
False
)
@classmethod
def
get
(
cls
,
pk
,
using
=
None
):
def
get
(
cls
,
pk
,
using
=
None
):
# pylint: disable=invalid-name
"""
Returns the model for the given primary key (pk).
"""
if
pk
is
None
:
return
None
return
get_instance
(
cls
,
pk
,
timeout
,
using
)
...
...
common
/djangoapps/cache_toolbox/relation.py
→
openedx/core
/djangoapps/cache_toolbox/relation.py
View file @
2c7975f8
...
...
@@ -69,18 +69,25 @@ Support
``cache_relation`` currently only works with ``OneToOneField`` fields. Support
for regular ``ForeignKey`` fields is planned.
"""
from
django.db.models.signals
import
post_save
,
post_delete
from
.core
import
get_instance
,
delete_instance
def
cache_relation
(
descriptor
,
timeout
=
None
):
"""
Adds utility methods to a model to obtain related
model instances via a cache.
"""
rel
=
descriptor
.
related
related_name
=
'
%
s_cache'
%
rel
.
field
.
related_query_name
()
@property
def
get
(
self
):
"""
Returns the cached value of the related model if found
in the cache. Otherwise gets and caches the related model.
"""
# Always use the cached "real" instance if available
try
:
return
getattr
(
self
,
descriptor
.
cache_name
)
...
...
@@ -93,10 +100,6 @@ def cache_relation(descriptor, timeout=None):
except
AttributeError
:
pass
# import logging
# log = logging.getLogger("tracking")
# log.info( "DEBUG: "+str(str(rel.model)+"/"+str(self.pk) ))
instance
=
get_instance
(
rel
.
model
,
self
.
pk
,
timeout
)
setattr
(
self
,
'_
%
s_cache'
%
related_name
,
instance
)
...
...
@@ -107,13 +110,24 @@ def cache_relation(descriptor, timeout=None):
# Clearing cache
def
clear
(
self
):
"""
Clears the cache of all related models of self.
"""
delete_instance
(
rel
.
model
,
self
)
@classmethod
def
clear_pk
(
cls
,
*
instances_or_pk
):
def
clear_pk
(
cls
,
*
instances_or_pk
):
# pylint: disable=unused-argument
"""
Clears the cache of all related models of
the provided instances_or_pk.
"""
delete_instance
(
rel
.
model
,
*
instances_or_pk
)
def
clear_cache
(
sender
,
instance
,
*
args
,
**
kwargs
):
def
clear_cache
(
sender
,
instance
,
*
args
,
**
kwargs
):
# pylint: disable=unused-argument
"""
Clears the cache of all related models of the
given instance.
"""
delete_instance
(
rel
.
model
,
instance
)
setattr
(
rel
.
parent_model
,
'
%
s_clear'
%
related_name
,
clear
)
...
...
common
/djangoapps/cache_toolbox/templatetags/__init__.py
→
openedx/core
/djangoapps/cache_toolbox/templatetags/__init__.py
View file @
2c7975f8
File moved
common
/djangoapps/cache_toolbox/templatetags/cache_toolbox.py
→
openedx/core
/djangoapps/cache_toolbox/templatetags/cache_toolbox.py
View file @
2c7975f8
"""
Implementation of custom django template tags for
automatically caching template fragments.
"""
from
django
import
template
from
django.core.cache
import
cache
from
django.template
import
Node
,
TemplateSyntaxError
,
Variable
from
django.template
import
resolve_variable
register
=
template
.
Library
()
register
=
template
.
Library
()
# pylint: disable=invalid-name
class
CacheNode
(
Node
):
"""
Subclass of django's template Node class that
caches the rendered value of a template fragment. This is a
simpler implementation of django.templatetags.cache.CacheNode.
"""
def
__init__
(
self
,
nodelist
,
expire_time
,
key
):
self
.
nodelist
=
nodelist
self
.
expire_time
=
Variable
(
expire_time
)
...
...
@@ -46,6 +55,11 @@ def cachedeterministic(parser, token):
class
ShowIfCachedNode
(
Node
):
"""
Subclass of django's template Node class that
renders the cached value for the given key, only
if already cached.
"""
def
__init__
(
self
,
key
):
self
.
key
=
key
...
...
@@ -55,7 +69,7 @@ class ShowIfCachedNode(Node):
@register.tag
def
showifcached
(
parser
,
token
):
def
showifcached
(
parser
,
token
):
# pylint: disable=unused-argument
"""
Show content if it exists in the cache, otherwise display nothing.
...
...
common
/djangoapps/cache_toolbox/tests/__init__.py
→
openedx/core
/djangoapps/cache_toolbox/tests/__init__.py
View file @
2c7975f8
File moved
common
/djangoapps/cache_toolbox/tests/test_middleware.py
→
openedx/core
/djangoapps/cache_toolbox/tests/test_middleware.py
View file @
2c7975f8
File moved
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment