Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expiring specific session variable in Django

It's easy to expire the request.session dictionary via request.session.set_expiry(seconds_until_end_of_day) or via SESSION_COOKIE_AGE.

But how do I set the expiry of a specific session variable? E.g. I've alloted an unauth user a temp_id. I need this temp_id to cease to exist in 30 mins. What's the best, most performant way to achieve that?

like image 347
Hassan Baig Avatar asked Oct 22 '25 04:10

Hassan Baig


1 Answers

How about putting it in the cache instead

from django.core.cache import cache
cache.set('{0}_temp_id'.format(request.session.session_key, value, timeout

This is uniquely associated with the user and automatically expires. No need for complicated middleware

like image 58
e4c5 Avatar answered Oct 25 '25 00:10

e4c5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!