django-redis-cache

越欣怡
2023-12-01
2.5 django缓存优化
	----目录---
	6.站点和view缓存
		settings.py
		#CACHE_MIDDLEWARE_ALIAS = 'default'
		CACHE_MIDDLEWARE_SECONDS = 3000
		CACHE_MIDDLEWARE_KEY_PREFIX = 'SiteCache'
		1.
		站点缓存:
		MIDDLEWARE_CLASSES=(
			'django.middleware.cache.UpdateCacheMiddleware', #第一
			'django.middleware.cache.CommonMiddleware',
			....
			'django.middleware.cache.FetchFromCacheMiddleware', #最后
		)
		2.1
		view缓存,cache_page 装饰器会自动缓存view的response
		from django.views.decorators.cache import cache_page
		@cache_page(60*5,key_prefix='my_view_cache')
		def my_view(request):
			....
		2.2 也可以在URLconf中使用
		urlpatterns = [
			url(r'^admin/', admin.site.urls),
			url(r'^home/(\d+)/$',cache_page(50*6,key_prefix='home_cache')(home)),
			]
	7. Template 与 Low-level 缓存
		至少需要两个参数 
		- timeout
		- name :缓存片段名称
		{% load cache %}
		<!--base.html-->
		{% block base_navigation_bar %}
			{% cache 60 navigation_bar  using='cache_alias' %}
				{%  for category in category_list  %}
					<li><a href="{% url 'category' %}?id={{ category.id }}">{{ category.name }}</a></li>
				{% endfor %}
			{% endcache %}
		{% endblock %}
	8.redis + django
		ex 过期时间秒 px 过期时间毫秒
		nx key不存在时才设置K-V
		xx key存在时才设置K-V
		django 方法对应 redis命令
		def set 		-> set/setes
		def get			-> get
		def add			-> setnx/expire
		def set_many	-> mset/setex
		def get_many	-> mget
		def incr		-> exists/incrby/get/set/setex
		def decr		-> def incr
		def has_key		-> exists
		def ttl			-> exists/ttl
		def expire		-> expire	#设置过期时间
		def persist		-> persist	#取消过期时间
		def delete		-> del
		def clear		-> flushdb/del
	9.django-redis 
		pip install django-redis-cache
		settings.py
			LOCATION 字段:
				127.0.0.1:6379
				redis://[:pwd]@127.0.0.1:6379/0
		----
		CACHES = {
			'default': {
				'BACKEND': 'redis_cache.RedisCache',
				'LOCATION': 'redis://@192.168.1.7:6379/0',
				'OPTIONS': {
					# 'DB': 0,
					# 'PASSWORD':  '',
					# 'PARSER_CLASS': 'redis.connection.HiredisParser',
					# 'CONNECTION_POOL_CLASS': 'redis.BlockingConnectionPool',
					# 'PICKIE_VERSION': '-1',
				},
				'KEY_PREFIX': 'blog_',
			}
		}
		urls.py
		----# 1. view 级别的缓存
		urlpatterns = [
			url(r'^$', cache_page(60*5)(index), name='index'),
			...
		]
		或者views.py
		@cache_page(60*5,key_prefix='my_view_cache')
		def index(request):
			...
		
		----templates 缓存
		html文件 
		{{ load cache }}
		{% cache 300 sidbar  using='cache_alias' %}
			...
		{% endcache %}
	10.django redis 性能与优化
		django-debug-toolbar
		jmeter 压力测试
	@cached_property 装饰器 一个方法被多次调用并做同样的事情,可以缓存其返回值 
	-----------
	https://docs.djangoproject.com/en/1.8/topics/cache/
	pip install django_extensions
	settings.py
	----
	CACHES = {
		'default':{
			'BACKEND':'django.core.cache.backends.memcached.MemcachedCache',
			'LOCATION':'127.0.0.1:6789'
		}
		#'default':{
		#	'BACKEND':'django.core.cache.backends.locmem.LocMemCache',
		#}
		
	}
	----使用
	from django.core.cache import cache
	cache.set('k','hello,world',30) #30s超时
	cache.get('k')
	----Caching API----
	per-site
	per-view
	python-object
	template-fragment

 类似资料: