本文整理汇总了Python中django.core.cache.cache.set方法的典型用法代码示例。如果您正苦于以下问题:Python cache.set方法的具体用法?Python cache.set怎么用?Python cache.set使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.cache.cache
的用法示例。
在下文中一共展示了cache.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: model_from_slug
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def model_from_slug(product_line, model=None):
"""
Returns product description for model slug or models dict for
the specified product line
"""
if not cache.get("slugmap"):
slugmap = {} # Map model slug to corresponding product description
product_lines = gsxws.products.models()
for k, v in product_lines.items():
d = {}
for p in v['models']:
slug = slugify(p)
d[slug] = p
slugmap[k] = d
cache.set("slugmap", slugmap)
models = cache.get("slugmap").get(product_line)
if model is not None:
return models.get(model)
return models
示例2: add_cleanup_pod
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def add_cleanup_pod(url):
"""populate the cleanup pod list"""
# variance allows a pod to stay alive past grace period
variance = random.uniform(0.1, 1.5)
grace = round(settings.KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS * variance)
# save
pods = cache.get('cleanup_pods', {})
pods[url] = (datetime.utcnow() + timedelta(seconds=grace))
cache.set('cleanup_pods', pods)
# add grace period timestamp
pod = cache.get(url)
grace = settings.KUBERNETES_POD_TERMINATION_GRACE_PERIOD_SECONDS
pd = datetime.utcnow() + timedelta(seconds=grace)
timestamp = str(pd.strftime(MockSchedulerClient.DATETIME_FORMAT))
pod['metadata']['deletionTimestamp'] = timestamp
cache.set(url, pod)
示例3: add_cache_item
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def add_cache_item(url, resource_type, data):
cache.set(url, data, None)
# Keep track of what resources are in a given resource
items = cache.get(resource_type, [])
if url not in items:
items.append(url)
cache.set(resource_type, items, None)
# Keep track of what resources exist under other resources (mostly namespace)
# sneaky way of getting data up to the resource type without too much magic
cache_url = ''.join(url.partition(resource_type)[0:2])
items = cache.get(cache_url, [])
if url not in items:
items.append(url)
cache.set(cache_url, items, None)
示例4: remove_cache_item
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def remove_cache_item(url, resource_type):
# remove data object from individual cache
cache.delete(url)
# get rid of log element as well for pods
if resource_type == 'pod':
cache.delete(url + '_log')
# remove from the resource type global scope
items = cache.get(resource_type, [])
if url in items:
items.remove(url)
cache.set(resource_type, items, None)
# remove from namespace specific scope
# sneaky way of getting data up to the resource type without too much magic
cache_url = ''.join(url.partition(resource_type)[0:2])
items = cache.get(cache_url, [])
if url in items:
items.remove(url)
cache.set(cache_url, items, None)
示例5: get_annotation_tree
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def get_annotation_tree():
ann_path = ServerSettings.annotation_path(None)
filename = os.path.join(ann_path, 'covered.json')
if not os.path.exists(filename):
return None
tree = cache.get('ui_ann', None)
if tree is None:
tree = XPathTree('/', None)
with open(filename, 'r') as f:
profile = json.load(f)
for line in profile.get('data', []):
tree.insert(line, profile.get('annotate', None))
cache.set('ui_ann', tree)
else:
print 'From cache..'
return tree
示例6: annotate
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def annotate(nodes, tree=None):
"""
Args:
nodes: list lxml element tree nodes with lazy tree instance
Returns:
Annotated nodes with attribute and value specified in annotation file
"""
if not tree:
tree = get_annotation_tree()
if tree and nodes:
for node in nodes:
xpath = node.get('path', '')
instance = tree.search(xpath)
if not instance:
continue
for attr, value in instance.attrib.items():
node.set(attr, value)
annotate(list(node), tree)
return nodes
示例7: do_photo_fetch
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def do_photo_fetch(emplids):
"""
Do the actual work of fetching photos: called by the celery task. Store them in the cache for finding by the view.
"""
photos = _get_photos(emplids)
for emplid in photos:
cache.set(_photo_cache_key(emplid), photos[emplid], 3600*24)
missing = set(emplids) - set(photos.keys())
if missing:
# some images missing: cache the failure, but not for as long
data = open(DUMMY_IMAGE_FILE, 'rb').read()
for emplid in missing:
cache.set(_photo_cache_key(emplid), data, 3600)
result = list(set(photos.keys()))
logger.debug("do_photo_fetch(%r) returning %r" % (emplids, result))
return result
# functions that actually get photos
示例8: get_wikitext
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def get_wikitext(self):
"""
Return this version's markup (reconstructing from diffs if necessary).
Caches when reconstructing from diffs
"""
if self.diff_from:
key = self.wikitext_cache_key()
wikitext = cache.get(key)
if wikitext:
return str(wikitext)
else:
src = self.diff_from
diff = json.loads(self.diff)
wikitext = src.apply_changes(diff)
cache.set(key, wikitext, 24*3600) # no need to expire: shouldn't change for a version
return str(wikitext)
return str(self.wikitext)
示例9: html_contents
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def html_contents(self, offering=None):
"""
Return the HTML version of this version's wikitext (with macros substituted if available)
offering argument only required if self.page isn't set: used when doing a speculative conversion of unsaved content.
Cached to save frequent conversion.
"""
key = self.html_cache_key()
html = cache.get(key)
if html:
return mark_safe(html)
else:
markup_content = self.substitute_macros(self.get_wikitext())
html = markup_to_html(markup_content, self.markup(), pageversion=self, html_already_safe=True)
cache.set(key, html, 24*3600) # expired if activities are changed (in signal below), or by saving a PageVersion in this offering
return mark_safe(html)
# signal for cache invalidation
示例10: super_units
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def super_units(self, include_self=False):
"""
Units directly above this in the heirarchy
"""
key = 'superunits-' + self.slug
res = cache.get(key)
if res:
if include_self:
return res + [self]
else:
return res
else:
res = self.__super_units()
cache.set(key, res, 24*3600)
if include_self:
return res + [self]
else:
return res
示例11: import_joint
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def import_joint(extra_where='1=1'):
"""
Find combined sections and set CourseOffering.config['joint_with'] appropriately.
"""
db = SIMSConn()
db.execute("SELECT strm, class_nbr, sctn_combined_id FROM ps_sctn_cmbnd c WHERE c.strm IN %s "
" AND ("+extra_where+")", (import_semesters(),))
for k,v in itertools.groupby(db, lambda d: (d[0], d[2])):
# for each combined offering...
strm, _ = k
class_nbrs = [int(class_nbr) for _,class_nbr,_ in v]
offerings = CourseOffering.objects.filter(semester__name=strm, class_nbr__in=class_nbrs)
for offering in offerings:
offering.set_joint_with([o.slug for o in offerings if o != offering])
offering.save()
示例12: escape_arg
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def escape_arg(self, a):
"""
Escape argument for DB2
"""
# Based on description of PHP's db2_escape_string
if type(a) in (int,int):
return str(a)
if type(a) in (tuple, list, set):
return '(' + ', '.join((self.escape_arg(v) for v in a)) + ')'
# assume it's a string if we don't know any better
a = str(a)
a = a.replace("\\", "\\\\")
a = a.replace("'", "\\'")
a = a.replace('"', '\\"')
a = a.replace("\r", "\\r")
a = a.replace("\n", "\\n")
a = a.replace("\x00", "\\\x00")
a = a.replace("\x1a", "\\\x1a")
return "'" + a + "'"
示例13: cache_by_args
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def cache_by_args(func, seconds=38800): # 8 hours by default
"""
Decorator to cache query results from SIMS (if successful: no SIMSProblem).
Requires arguments that can be converted to strings that uniquely identifies the results.
Return results must be pickle-able so they can be cached.
"""
def wrapped(*args, **kwargs):
key = "simscache-" + func.__name__ + "-" + _args_to_key(args, kwargs)
# first check cache
cacheres = cache.get(key)
if cacheres:
return cacheres
# not in cache: calculate
res = func(*args, **kwargs)
# got a result (with no exception thrown): cache it
cache.set(key, res, seconds)
return res
wrapped.__name__ = func.__name__
return wrapped
示例14: disabled_features
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def disabled_features():
"""
Get the current set of disabled features: from the Django cache if possible, or from the loader if not.
"""
if cache_timeout:
# check the cache and return it if we find it
flags = cache.get(cache_key)
if flags is not None:
return flags
# not in the cache: have the loader find it
loader = get_loader()
flags = loader.get_disabled_features()
if cache_timeout:
cache.set(cache_key, flags, cache_timeout)
return flags
示例15: program_as_of
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set [as 别名]
def program_as_of(self, semester=None, future_if_necessary=False):
if semester == None:
semester = Semester.current()
gph = GradProgramHistory.objects.filter(student=self, start_semester__name__lte=semester.name) \
.order_by('-start_semester', '-starting').select_related('program').first()
if not gph and future_if_necessary:
# look into the future for the program the *will* be in: that's how we'll set gs.program earlier.
gph = GradProgramHistory.objects.filter(student=self) \
.order_by('start_semester', '-starting').select_related('program').first()
if gph:
return gph.program
else:
return None