采取什么方法来为用户缓存独特的自动建议列表?
我正在制作一个应用程序(asp.net/c#),它将自动建议用户输入的几个字段。每个用户最终都会构建自己的自动建议列表。 每次他们添加一个项目时,如果它是一个新单词,它就会被添加到自动建议列表中,就像 gmail 一样。
我想知道大多数人都是怎么做的? 每次按键都调用服务器似乎效率不高? 我应该为每个用户创建一个包含一个条目的巨大 xml 文件吗? 每个用户都有一个 xml 文件? 我将如何缓存它以使其高效?
各种各样的问题,但我基本上寻找的是最佳实践。 谢谢。
I am making an app (asp.net/c#) that will autosuggest a couple fields that users type in. Each user will end up building their own auto-suggest list. Every time they add an item, if it's a new word, it will get added to their list of auto-suggests, just like gmail.
I was wondering how most people go about this? Making a call to the server for every key-press doesn't seem very efficient? Should I make a huge xml file with one entry for each user? An xml file for each user? how would I cache this to make it efficient?
All sorts of questions, but what I am basically looking for is best practices. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
数据存储
您应该将此信息存储在数据库中。 创建一两个表来保存每个用户每个字段的此类信息。
客户端查询
当用户开始在文本字段中输入内容时,您可以从客户端(通过 AJAX)查询数据库。 如果您这样做,最好的做法是等到字段中至少有 3 个(左右)字符后再向服务器查询建议。 这会稍微减少请求数量以及客户端和服务器之间的流量。
我将这种方法用于存在大量可能的自动建议命中的字段。
缓存
如果您使用此方法,则可以通过在某些情况下缓存响应来优化流程。
假设仅在文本字段中包含 3 个字符后才触发查询,则您可以缓存整个结果。 添加到文本框中的任何其他字符都不会增加返回的结果数。 因此,您可以继续过滤缓存的结果。 但是,如果用户删除了某个字符,您可以刷新缓存以确保您拥有最合适的自动建议命中。
服务器端预加载
在服务器端,收集所有这些数据并将其与页面请求一起发送。 (您可以将其作为 JavaScript 数组推送到可以在文本字段的
onchange
事件中引用的位置。这样,您就不必进行任何 AJAX 调用。这似乎更好对于不会有大量可能的自动建议命中的字段。
Data Storage
You should store this information in a database. Create a table or two to hold this type of information per user per field.
Client-side Query
You can query the database from the client side (via AJAX) when the user starts to type in the text field. If you do this, it is considered best practice to wait until there are at least 3 (ish) characters in the field before querying the server for suggestions. This reduces the number of requests slightly and the traffic between the client and server.
I use this method for fields where there is a large number of possible auto-suggest hits.
Caching
If you use this method, you can optimize the process by caching the response in certain situations.
Assuming that the query is fired off only after 3 characters are in the text field, you can then cache the entire result. Any further characters added to the text box will not increase the number of results returned. So, you can just continue to filter down the cached results. If, however, the user deletes a character, you can refresh the cache to make sure that you have the most appropriate auto-suggest hits available.
Server-side Preloading
On the server-side, gather all of this data and send it along with the page request. (You can push it down as a javascript array somewhere that can be referenced in the
onchange
event of the text field. This way, you don't have to make any AJAX calls.This appears to be better for fields where there will not be a large number of possible auto-suggest hits.
我会将这些值存储在数据库中(您可能会在其中存储用户配置文件),并在用户登录时将这些值提取到 ASP.NET 会话中。然后只需使用会话中的值作为自动完成值。 这样,当他们登录时,您只需访问数据库一次。
I would store the values in a database (where you are storing their user profiles maybe), and pull the values into ASP.NET Session when the user logs in. Then just use the values from the Session for the auto-complete values. This way you will only have to hit the database once when they log in.
Chuck Norris 建议,根据您的 JavaScript 框架,您可以在进行服务器调用之前构建客户端检查,以查看字段中是否写入了给定数量的字符或单词。 应根据给定用户在数据库中输入的信息,使用自动建议词动态生成 XML 文件。
根据您尝试构建的应用程序,对特定数据库字段执行全文 SQL 查询可能会更容易。 为了提高性能,您可以构建一个由客户端浏览器缓存的 cookie,并让您的应用程序检查更改或在执行完整数据库查询之前检查它是否存在。
Chuck 不建议使用会话变量,因为这会降低应用程序的性能,因为它必须为每个用户在内存中保留这些自动建议值。
查克想知道在他交出我的大脑之前你是否还需要什么。
Chuck Norris recommends, depending on your JavaScript framework, that you can build client-side checks before making a server call to see if there are a given number of characters or words written in the field. The XML file should be dynamically generated with the auto-suggest words for a given user based on the information entered by that user in the database.
Depending on the application that you are trying to build it may be easier to perform full-text SQL queries on specific database fields. For performance you could probably build a cookie that will be cached by the clients' browser and have your application check for changes or check to see if it exists before performing a full database query.
Chuck does not recommend using session variables because you would degrade the performance of your application because it has to keep those auto-suggest values in memory for each user.
Chuck would like to know if you need anything else before he surrenders my brain.