Django project learning 5-- registration function 2, session and redis cache validation code (running account)

Plus front uuid utilizes graphical verification code to verify, codes generally have a expiration time. Let's set an expiration time.

def Image_code(request, img_id):
	text, image = captcha.generate_captcha()
	# 配置数据库连接
	redis_conn = get_redis_connection('verify_code')
	# 保存
	# redis_conn.setex('img_{}'.format(img_id).encode('utf8'),10,text)
	# 设置过期时间
	request.session['image_code'] = text
	request.session.set_expiry(60)
	return HttpResponse(image, content_type='image/jpg')

Here Insert Picture Description
The verification code to the session cache inside good inside than redis database into, because each refresh, must connect to the database, and into the session which can reduce the consumption of the database. (Most popular Web site, then a large database overhead)

To mention a way to cache the session redis inside.
Configuration:
Here Insert Picture Description

Released eight original articles · won praise 0 · Views 107

Guess you like

Origin blog.csdn.net/weixin_43129747/article/details/104614041