简体   繁体   English

Django缓存键命名

[英]django cache key naming

I want to store some items using Django cache API. 我想使用Django缓存API存储一些项目。 Are there are best practices to follow while naming the key. 命名密钥时是否遵循最佳实践? I know some people just give user name as the key. 我知道有些人只是将用户名作为密钥。 But I am going to cache various items in different views and having the same key every where is not feasible. 但是我将在不同的视图中缓存各种项目,并在所有不可行的地方都使用相同的密钥。 I was thinking on may be giving a key with username+ 'some view specific' so that the key can be unique. 我在想,可能是给一个带有用户名+“某些视图特定”的密钥,以便该密钥可以是唯一的。

Does any one have any other good suggestions for generating keys? 有人对生成密钥有其他好的建议吗?

Generation of keys can depend on what you are tying to achieve. 密钥的生成取决于您要实现的目标。

  1. Is what the user is trying to access for that user only? 用户仅尝试访问该用户吗?
  2. Is what the user is trying to access generic for all the users? 用户正在尝试为所有用户访问通用吗?

eg 例如

let's say you are trying to access a url: 假设您正在尝试访问网址:

http://yourserver/endpoint/?filter1=value1&filter2=value2

In the above case, you can use the query params filter1=value1&filter2=value2 to create a cached key (by generating the md5 hash). 在上述情况下,可以使用查询参数filter1=value1&filter2=value2创建缓存的键(通过生成md5哈希值)。

Considering the two options earlier, if the view should return some data specific to the user then you can also append the user id to create a unique key for the user. 前面考虑了这两个选项,如果视图应该返回特定于用户的数据,那么您还可以附加user id来为user id创建唯一键。

Another example could be a url like this, where one is trying to access all the articles from source 1 : 另一个示例可能是这样的网址,其中有人试图从source 1访问所有文章:

http://yourserver/source/1/articles/?filter1=value1&filter2=value2 

In this case it might also be useful to append the cache key with the source id (so this uses the context data for the views in generating the keys). 在这种情况下,将cache key附加source id可能也很有用(因此在生成键时,它将上下文数据用于视图)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM