如何使用 ajax、php 或 jquery 缓存信息?
我试图了解缓存数据的使用。我对此有所了解。它有助于更快地加载数据,因为它将信息存储在浏览器中?
如果有人可以帮助我更好地理解这一点,我将不胜感激。另外,是否可以缓存输入字段中的值并检索它或存储它以供以后使用?如果是这样,有人可以向我展示如何在 PHP、ajax 或 jquery 中实现它的示例,或者指导我学习教程吗?
谢谢!
I'm trying to understand the use of caching data. i know a little bit about it. It helps data load faster because its stores the information in the browser?
If someone can help me understand this better, i'll be grateful. Also, is it possible to cache values from an input field and retrieve it or store it for later use? If so, can someone show me a sample of how it can be implemented in PHP, ajax, or jquery or guide me to a tutorial?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这一季的流行是使用localStorage在客户端机器上存储数据。
例如,请参见: http://php-html.net/tutorials/html5 -本地存储指南/
This season's fashion is to use localStorage for storing data on client's machine.
See for example this: http://php-html.net/tutorials/html5-local-storage-guide/
缓存是位于数据源(数据库、硬盘驱动器、Web 资源等)和需要访问该数据源的东西之间的东西。这个想法是,从缓存访问数据应该比直接从数据源访问数据更快。
如果您想存储文本字段值以供以后使用(不了解更多有关您的设置的细节),我认为您正在寻找 cookie。
当用户在文本框中输入文本时,使用 javascript 设置 cookie。
下次用户访问该页面时,您可以使用 javascript 检索他们在文本框中输入的内容,以查看 cookie 是否存在,如果存在,其中包含哪些内容。
请注意,此方法会将数据缓存在用户的计算机上 - 而不是您的 Web 服务器上。如果您想将这些值存储在 Web 服务器上,那么您需要在用户提交表单时将它们写入文件或数据库,或者使用 AJAX 将输入表单的值异步发送到 PHP 后端,以便它可以将值存储在那里。
如果您可以更具体地说明您想要缓存/存储的什么以及您认为应该这样做的原因,那么我们可以为您提供更高质量的答案以及一些代码示例。
请准确描述您的问题是什么,以及您期望解决方案做什么。
编辑:从您的上一个问题来看,您似乎想要一些某种自动完成功能。有两种类型:
1) 自动完成用户先前输入的内容:
当用户提交表单时,您可以执行以下 3 项操作之一:
a) 将文本输入存储在数据库中。当用户下次访问该页面时,使用 PHP 将之前的搜索嵌入到 javascript 中(可能作为数组)。当用户在文本框中键入内容时,使用 javascript 显示自动完成框。 (对于用户来说更快,但如果您希望存储每个用户的大量先前输入,则不太实用)
b) 将文本输入存储在数据库中。当用户访问该页面时,使用 AJAX 在他们键入时检索最可能的匹配项。 (对于用户来说速度较慢,但如果您希望每个用户保存大量搜索,则更好)
c) 将文本输入存储在 cookie 中。当用户提交表单时,将输入保存在 cookie 中。您可以在多个 cookie 中存储多个搜索,也可以在 1 个分隔 cookie 中存储 1 个搜索。请注意,cookie 的最大大小(包括其名称和其他详细信息)为 4K,因此您应将其主体保持在 4000 字节以下。
注意:如果这样配置,浏览器应该自动自动完成用户之前输入的内容。您想要自己实现这种类型的自动完成的唯一原因是您想要某种跨浏览器或跨系统功能。
2) 预测自动完成,就像进行 Google 搜索时一样。
此方法本质上与 1b 相同。您将在数据库中存储以前或建议的搜索词的列表。当用户在输入字段中键入文本时,您的 AJAX 请求将获取用户正在搜索的最有可能的候选内容。
A cache is something which sits between a data source (database, hard drive, web resource, etc) and something that needs to access that data source. The idea is that accessing the data from the cache should be faster than accessing the data directly off the data source.
If you want to store text field values for use later, (without knowing more about the specifics of your setup), I think you're looking for a cookie.
When the user types text into the textbox, use javascript to set a cookie.
The next time the user visits the page, you can retrieve the things they have typed into the text boxes by using javascript to see if the cookie exists and if so, what's in them.
Note that this method will cache the data on the user's computer - not on your web server. If you want to store these values on the web server then you will need to write them to a file or database when the user submits the form, or use AJAX to send the value of the input form to your PHP back-end asynchronously, so it can store the value there.
If you can be a little more specific as to what you want to cache/store and why you think you should be doing it, then we can give you a better quality answer with some code examples.
Please describe exactly what your problem is, and what you expect the solution to do.
Edit: From your previous question it seems like you want some sort of auto-complete functionality. There are two-types:
1) Auto-complete what the user has previously typed:
When the user submits the form, you can do one of 3 things:
a) Store the text input in a database. When the user next visits the page, use PHP to embed the previous searches in the javascript (probably as an array). As the user types in the text-box, use javascript to display the auto-complete box. (Faster for the user, not so practical if you are expecting to store a huge amount of previous inputs per user)
b) Store the text input in a database. When the user visits the page, use AJAX to retreive the most likely match as they type. (Slower for the user, but better if you're expecting lots of saved searches per user)
c) Store the text input in a cookie. When the user submits the form, save the input in a cookie. You can either store multiple searches in multiple cookies, or 1 search in 1 delimited cookie. Note that the maximum size for a cookie (including it's name and other details is 4K, so you should keep it's body under 4000 bytes).
NOTE: If configured that way, the browser should automatically auto-complete what the user has previously typed. The only reason you would want to implement this type of auto-complete yourself is if you want some sort of cross-browser or cross-system functionality.
2) Predictive auto-complete like when doing a Google search.
This method essentially works the same as 1b. You will have stored in your database a list of previous or suggested search terms. As the user types text into the input field your AJAX request will fetch the most likely candidate for what the user is searching for.
这取决于。你几乎每次都可以缓存图像、css、js,因为它们总是相同的。您不需要执行任何操作,它们应该会自动缓存。
您还可以缓存 GET 和 POST 响应,但这有点负责任。您必须知道应该冲洗什么、何时以及多久冲洗一次。
要单独打开 jQuery GET 请求的缓存,请使用
It depends. You can cache images, css, js almost everytime because it is always the same. You do not need to do anything, they should cache automatically.
You can also cache GET and POST responses, but that's a bit responsible. You have to know what, when and how often should you flush it.
To turn on cache on GET requests by jQuery alone, use