在 cfquery 中使用 cachedwithin 属性 [英] Using cachedwithin attribute inside cfquery

查看:7
本文介绍了在 cfquery 中使用 cachedwithin 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在 cfquery 中使用 cachedwithin 属性时,它如何将查询存储在内存中.它是否仅按您分配给查询的名称存储它?例如,如果在我的索引页面上我将一个查询缓存了一个小时并将其命名为 getPeople 将在不同页面(或相同页面)上使用相同名称的查询使用缓存结果还是使用一些更好的逻辑来判断它是否是同一个查询?

When you use the cachedwithin attribute in a cfquery how does it store the query in memory. Does it store it by only the name you assign to the query? For example, if on my index page I cache a query for an hour and name it getPeople will a query with the same name on a different page (or the same page for that matter) use the cached results or does it use some better logic to decide if it is the same query?

另外,如果您的查询中有变量,缓存是否会考虑变量的值?

Also, if there is a variable in your query does the cache take into account the value of the variable?

推荐答案

这不仅仅是名称 - 它是您正在运行的确切查询.

It's not only the name -- it's the exact query you're running.

<cfquery name="getPeople" cachedwithin=".5" ...>
select name from employee order by name
</cfquery>

如果您在应用中的其他任何位置调用相同的查询,如果它在第一次查询的半天内,您将获得缓存版本.但这些将在数据库中获取新数据:

If you invoke this same query anywhere else in your app, you'll get the cached version if it's within half a day of the first query. But these will hit the database for fresh data:

<!--- Different name, same SQL: A new cached query --->
<cfquery name="getEmployees" cachedwithin=".5" ...>
select name from employee order by name
</cfquery>

<!--- Different SQL, same name: Redefining the cached query --->
<!--- Note: As pointed out in comments, it's not really overwriting the old query
      of the same name, but making a new one in the cache. The first one by the
      same name is still in the cache, waiting for eviction. --->
<cfquery name="getPeople" cachedwithin=".5" ...>
select name from employee order by name desc
</cfquery>

是的,它确实考虑了一个变量.如果你使用 cfqueryparam - 你应该这样做 - 你的数据库将缓存 查询计划,但即使使用 cachedwithin,每个查询从查询缓存的角度来看,更改的参数将被视为不同.请注意,这意味着如果您在使用不同参数多次运行的查询上使用 cachedwithin,您的查询缓存将被缓存命中率较低的查询淹没.

And yes, it does take a variable into account. If you use cfqueryparam -- which you should be doing -- your database will cache the query plan, but even using cachedwithin, each query with a changed parameter will be treated as different from a query caching perspective. Note that this means if you use cachedwithin on a query that runs many times with different parameters, you'll be flooding your query cache with queries that have low cache hit rates.

这篇关于在 cfquery 中使用 cachedwithin 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆