DJANGO 持久站点宽内存
我是 Django 新手,可能以不正常的方式使用它。 也就是说,我想找到一种拥有站点范围内存的方法。 解释一下。
我有一个非常简单的设置,其中一台计算机每隔几秒钟就会向该网站发布帖子。 我希望这些数据保存在某个地方。 我希望每个浏览该网页的人都可以通过一些 JavaScript 近乎实时地查看基于此数据的更新。
因此使用下面的示例代码。 计算机 A 会向 set_data 发送帖子并将数据设置为“数据集” 计算机 B、C、D 等...然后将获取 get_data 并查看“数据集” 不幸的是B、C、D只看到“”
我有一种感觉我需要的是memcached,但我在hostgator共享服务器上并且无法安装它。与此同时,我只是将它们写入文件。这可行,但确实效率低下,我希望为大量用户群提供服务。
感谢您的任何帮助。
#view.py
data=""
def set_data(request):
data = request.POST['data']
return HttpResponse("");
def get_data(request):
return HttpResponse(data);
I am new to Django, and probably using it in a way thats not normal.
That said, I would like to find a way to have site wide memory.
To Explain.
I have a very simple setup where one compter will make posts to the site every few seconds.
I want this data to be saved off somewhere.
I want everyone who is viewing the webpage to see updates based on this data in near real time via some javascript.
So using the sample code below.
Computer A would do a post to set_data and set data to "data set"
Computer B,C,D,etc.... would then do a get to get_data and see "data set"
Unfortunatly B,C,D just see ""
I have a feeling what i need is memcached, but I am on a hostgator shared server and cannot install that. In the meantime I am just writing them to files. This works but is really inneficient, and I am hopeing to serve a large user base.
Thanks for any help.
#view.py
data=""
def set_data(request):
data = request.POST['data']
return HttpResponse("");
def get_data(request):
return HttpResponse(data);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
memcached 是有损的,因此不能实现“持久”。
文件没问题,但切换到通过
mmap
访问它们。memcached is lossy, hence doesn't fulfil "persistent".
Files are fine, but switch to accessing them via
mmap
.持久存储也称为数据库(尽管在某些情况下 Django 的缓存后端也可能起作用)。永远不要尝试在 Web 开发中使用全局变量。
您是否应该使用 Django 模型或缓存后端实际上取决于您的用例,但您只是描述了一个人为的示例(或者您的 Web 应用程序是否包含 getter 和 setter?)。
Persistent storage is also called database (although for some cases Django's cache backend might work as well). Don't ever try to use global variables in web development.
Whether you should use a Django model or the cache backend really depends on your use case, but you just described a contrived example (or does your web app consist of a getter and a setter?).