本文整理汇总了Python中django.core.cache.cache.set_many方法的典型用法代码示例。如果您正苦于以下问题:Python cache.set_many方法的具体用法?Python cache.set_many怎么用?Python cache.set_many使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.cache.cache
的用法示例。
在下文中一共展示了cache.set_many方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_unicode
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def test_unicode(self):
# Unicode values can be cached
stuff = {
"ascii": "ascii_value",
"unicode_ascii": "Iñtërnâtiônàlizætiøn1",
"Iñtërnâtiônàlizætiøn": "Iñtërnâtiônàlizætiøn2",
"ascii2": {"x": 1},
}
# Test `set`
for (key, value) in stuff.items():
cache.set(key, value)
assert cache.get(key) == value
# Test `add`
for (key, value) in stuff.items():
cache.delete(key)
cache.add(key, value)
assert cache.get(key) == value
# Test `set_many`
for key, _value in stuff.items():
cache.delete(key)
cache.set_many(stuff)
for (key, value) in stuff.items():
assert cache.get(key) == value
示例2: test_binary_string
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def test_binary_string(self):
# Binary strings should be cacheable
from zlib import compress, decompress
value = "value_to_be_compressed"
compressed_value = compress(value.encode())
# Test set
cache.set("binary1", compressed_value)
compressed_result = cache.get("binary1")
assert compressed_value == compressed_result
assert value == decompress(compressed_result).decode()
# Test add
cache.add("binary1-add", compressed_value)
compressed_result = cache.get("binary1-add")
assert compressed_value == compressed_result
assert value == decompress(compressed_result).decode()
# Test set_many
cache.set_many({"binary1-set_many": compressed_value})
compressed_result = cache.get("binary1-set_many")
assert compressed_value == compressed_result
assert value == decompress(compressed_result).decode()
示例3: test_long_timeout
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def test_long_timeout(self):
"""
Using a timeout greater than 30 days makes memcached think
it is an absolute expiration timestamp instead of a relative
offset. Test that we honour this convention. Refs #12399.
"""
cache.set("key1", "eggs", 60 * 60 * 24 * 30 + 1) # 30 days + 1 second
assert cache.get("key1") == "eggs"
cache.add("key2", "ham", 60 * 60 * 24 * 30 + 1)
assert cache.get("key2") == "ham"
cache.set_many(
{"key3": "sausage", "key4": "lobster bisque"}, 60 * 60 * 24 * 30 + 1
)
assert cache.get("key3") == "sausage"
assert cache.get("key4") == "lobster bisque"
示例4: test_forever_timeout
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def test_forever_timeout(self):
"""
Passing in None into timeout results in a value that is cached forever
"""
cache.set("key1", "eggs", None)
assert cache.get("key1") == "eggs"
cache.add("key2", "ham", None)
assert cache.get("key2") == "ham"
added = cache.add("key1", "new eggs", None)
assert not added
assert cache.get("key1") == "eggs"
cache.set_many({"key3": "sausage", "key4": "lobster bisque"}, None)
assert cache.get("key3") == "sausage"
assert cache.get("key4") == "lobster bisque"
示例5: test_set_many_expiration
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def test_set_many_expiration(self):
# set_many takes a second ``timeout`` parameter
# Perform a single query first to avoid spurious on-connect queries
caches["no_cull"].get("nonexistent")
with self.assertNumQueries(1):
caches["no_cull"].set_many({"key1": "spam", "key2": "eggs"}, 0.1)
cache.set("key3", "ham")
time.sleep(0.2)
assert cache.get("key1") is None
assert cache.get("key2") is None
assert cache.get("key3") == "ham"
# set_many expired values can be replaced
with self.assertNumQueries(1):
caches["no_cull"].set_many(
{"key1": "spam", "key2": "egg", "key3": "spam", "key4": "ham"}, 1
)
v = cache.get("key1")
assert v == "spam"
assert cache.get("key2") == "egg"
assert cache.get("key3") == "spam"
assert cache.get("key4") == "ham"
示例6: set_user_course_permissions
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def set_user_course_permissions(user, courses):
"""
Sets which courses user is allowed to view.
Arguments
user (User) -- User for which permissions should be set
courses (list) -- List of course ID strings
"""
if not user:
raise ValueError('User not specified!')
if courses is None:
raise ValueError('Courses not specified!')
# Ensure courses are stored as a list.
courses = list(courses)
key_courses, key_last_updated = _get_course_permission_cache_keys(user)
data = {key_courses: courses, key_last_updated: datetime.datetime.utcnow()}
cache.set_many(data, settings.COURSE_PERMISSIONS_TIMEOUT)
示例7: cache
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def cache(seconds=60):
def decorator(func):
@wraps(func)
def wrapper(admin, model):
cache_method_key = helper.cache_method_key(model, func.__name__)
value = django_cache.get(cache_method_key)
if not value:
value = func(admin, model)
cache_object_key = helper.cache_object_key(model)
obj_methods_caches = django_cache.get(cache_object_key) or ''
django_cache.set_many({
cache_method_key: value,
cache_object_key: obj_methods_caches + '|' + cache_method_key
}, seconds)
return value
return wrapper
return decorator
示例8: get_cleaned_articles
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def get_cleaned_articles(articles) -> dict:
from_cache = cache.get_many([cache_id_to_key(a.id) for a in articles])
rv = {cache_key_to_id(k): v for k, v in from_cache.items()}
to_cache = dict()
for article in articles:
if article.id in rv:
continue
cleaned = html_processing.clean_article(
article.content,
base_url=article.feed.uri
)
rv[article.id] = cleaned
to_cache[cache_id_to_key(article.id)] = cleaned
if to_cache:
cache.set_many(to_cache, timeout=7200)
return rv
示例9: get_parts
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def get_parts(self):
"""
Returns GSX parts for a product with this device's serialNumber
"""
results = {}
cache_key = "%s_parts" % self.sn
for p in gsxws.Product(self.sn).parts():
product = Product.from_gsx(p)
results[product.code] = product
cache.set_many(results)
cache.set(cache_key, results.values())
return results.values()
示例10: handle
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def handle(self, *args, **options):
cache.set_many(dict((key, None) for key in args))
示例11: save_lookups
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def save_lookups(self, items):
"""
Cache the items we looked up for a small amount of time.
"""
cache.set_many(
{f"{self.CACHE_KEY}{key}": value for key, value in items.items()},
timeout=60,
)
示例12: test_zero_timeout
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def test_zero_timeout(self):
"""
Passing in zero into timeout results in a value that is not cached
"""
cache.set("key1", "eggs", 0)
assert cache.get("key1") is None
cache.add("key2", "ham", 0)
assert cache.get("key2") is None
cache.set_many({"key3": "sausage", "key4": "lobster bisque"}, 0)
assert cache.get("key3") is None
assert cache.get("key4") is None
示例13: test_delete_with_prefix_with_no_reverse_works
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def test_delete_with_prefix_with_no_reverse_works(self):
cache.set_many({"K1": "value", "K2": "value2", "B2": "Anothervalue"})
assert cache.delete_with_prefix("K") == 2
assert cache.get_many(["K1", "K2", "B2"]) == {"B2": "Anothervalue"}
示例14: test_cull_max_entries_minus_one
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def test_cull_max_entries_minus_one(self):
# cull with MAX_ENTRIES = -1 should never clear anything that is not
# expired
# one expired key
cache.set("key", "value", 0.1)
time.sleep(0.2)
# 90 non-expired keys
for n in range(9):
cache.set_many({str(n * 10 + i): True for i in range(10)})
cache.cull()
assert self.table_count() == 90
示例15: get_context_data
# 需要导入模块: from django.core.cache import cache [as 别名]
# 或者: from django.core.cache.cache import set_many [as 别名]
def get_context_data(self, *args, **kwargs):
context = super(InboxView, self).get_context_data(*args, **kwargs)
object_list = []
object_id_list = []
for email in context["page_obj"].object_list:
object_list.append(email)
object_id_list.append(email.id)
if len(object_id_list) == 0:
return context
headers = cache.get_many(object_id_list, version="email-header")
missing_list = set(object_id_list) - set(headers.keys())
if len(missing_list) > 0:
missing_headers = models.Header.objects.filter(part__parent=None, part__email__in=missing_list)
missing_headers = missing_headers.get_many("Subject", "From", group_by="part__email_id")
headers.update(missing_headers)
cache.set_many(missing_headers, version="email-header", timeout=None)
for email in object_list:
header_set = headers[email.id]
email.subject = header_set.get("Subject")
email.sender = header_set["From"]
inbox = getattr(self, 'inbox_obj', None)
if inbox is not None:
inbox = inbox.id
set_emails_to_seen.delay(object_id_list, self.request.user.id, inbox)
return context