什么是缓存? [英] What is caching?

查看:172
本文介绍了什么是缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



或者,如何在程序代码中执行x,y,z操作,损害了您的缓存能力。



即使在最新的播客之一,Jeff Atwood谈到他们如何缓存某些值以实现快速检索。



在cache和caching这两个术语中似乎存在一些歧义,导致我对它在不同情况下的意思感到困惑。无论你是指应用程序还是数据库缓存,cpu等等。



什么是缓存,什么是不同类型?


从上下文我可以得到一个感觉,
将一个oft检索值存储到
主内存中,有快速查看
访问它。



这个词似乎在许多不同的上下文中使用,它们的含义稍有不同cpu,数据库,应用程序等),我真的想要清除它。


如何在您的应用程序中缓存与您的数据库缓存相对应?


当有人说他们发现一个
代码,将损害缓存
,并在他们修复它后,它改善了
的应用程序的速度,他们是什么是
谈论?



程序的缓存东西
是自动完成吗?如何
允许值被缓存在
程序中?我经常读用户
这个网站说,他们缓存一个价值
在他们的应用程序,我坐在这里,
想知道他们的意思。



此外,当
有人谈论数据库
缓存时,真正意味着什么?这只是他们在他们的数据库中打开的功能
吗? do
你必须显式地缓存值或
数据库选择哪些到
缓存为你?


如何自行开始缓存项目以提高性能?


我如何开始在我的应用程序中缓存值?或者,这是已经完成的东西,在底层,我只需要写一个特定的方式来允许缓存的代码?



数据库缓存,我如何开始呢?我听说过像memcache的东西。是否需要在数据库中缓存这种类型的实用程序?


我希望在应用程序与数据库之间有一个很好的区别,如何使用它们以及如何在这两种情况下实现它们。

解决方案

缓存只是将数据存储和检索数据从高性能存储(通常是内存)显式或隐式。



让我解释一下。内存比文件,远程URL(通常),数据库或任何其他外部信息存储更快的访问。因此,如果使用这些外部资源之一的行为是重大,那么您可以从缓存中受益以提高性能。



Knuth曾说过早优化是所有邪恶的根源。那么,提前缓存是我所关心的所有头痛的根源。在您问题之前,请勿解决问题。你做出的每一个决定都需要付出代价来实现它,并再次支付,以便稍后更改,因此你可以推迟做出改变,改变系统的时间越长。



因此,首先确定您确实遇到问题,。分析,日志和其他形式的性能测试将在这里帮助您。我不能强调这一步是多么重要。我已经看到人们优化不是问题的事情的次数是惊人的。



好吧,所以你有一个性能问题。假设您的网页运行的查询需要很长时间。如果是读取,则您有多个选项:




  • 将查询作为单独的进程运行,并将结果放入缓存。所有页面只访问缓存。您可以酌情更新缓存版本(每天一次,每周一次,每5秒一次,任何适当的内容);

  • 通过持久性提供程序,ORM或随你。当然这取决于你使用什么技术。 Hibernate和Ibatis例如支持查询结果缓存;

  • 如果结果不在缓存中,您的页面运行查询(或者它的stale,意味着计算时间早于指定的age),并将其放入缓存。如果两个(或多个)单独的进程都决定他们需要更新结果,所以你最终一次运行相同(昂贵)查询八次,这会有并发问题。您可以处理此锁定高速缓存,但会创建另一个性能问题。您还可以回到您的语言中的并发方法(例如Java 5并发API)。



如果是更新需要反映在读取缓存中的位置),那么它会更复杂一些,因为在缓存中有一个旧值,而在数据库中有一个更新的值,这样您就可以为页面提供不一致的数据视图。但是广义上有四种方法:




  • 更新缓存,然后将请求排队更新相关商店;

  • 通过缓存写:缓存提供程序可以提供一种机制来持久化更新并阻止调用程序,直到做出更改;和

  • 后写式缓存:与直写式缓存相同,但不会阻止调用者。更新异步并单独发生;和

  • 持久性服务模型:这假设您的缓存机制支持某种可观察性(即缓存事件侦听器)。



以上哪一个是必须的您选择的方法将取决于您的要求,您使用的技术和其他一些因素(例如,是否需要群集和故障转移支持?)。



很难更具体,而且不知道太多有关您的问题的详细信息(例如您是否有问题),您可以在哪些方面做出指导。


I'm constantly hearing about person y had performance issue x which they solved through caching.

Or, how doing x,y,z in your programs code can hurt your caching ability.

Even in one of the latest podcasts, Jeff Atwood talks about how they cache certain values for speedy retrieval.

There seems to be some ambiguity in the terms "cache" and "caching" and it has led me to be confused about it's meaning in different cases. Whether you are referring to application or database caching, cpu, etc and what that means.

What is caching and what are the different types?

From context I can get a sense of it, to store an oft retrieved value into main memory and have quicklook up access to it. However, what is it really?

This word seems to be used in a lot of different contexts with slightly different meaning (cpu, database, application, etc) and I'm really looking to clear it up.

Is there a distinction between how caching works in your applications vs your database caching?

When someone says that they found a piece of code that would hurt caching and after they fixed it, it improved the speed of their app, what are they talking about?

Is the program's caching something that is done automatically? How do you allow values to be cached in your programs? I've often read users on this site say that they cached a value in their application, I sit here and wonder what they mean.

Also, what does it really mean when someone talks about database caching? Is this simply a feature they turn on in their database? Do you have to explicitly cache values or does the database pick which ones to cache for you?

How do I begin caching items myself to improve performance?

Can you give me some examples of how I can begin caching values in my applications? Or again, is this something that is already done, under the hood and I simply have to write my code in a particular way to allow "caching"?

What about database caching, how do I begin that? I've heard about things like memcache. Is this type of utility required to cache in databases?

I'm looking to get a good distinction between caching in applications vs databases, how they are used and how it is implemented in both cases.

解决方案

Caching is just the practice of storing data in and retrieving data from a high-performance store (usually memory) either explicitly or implicitly.

Let me explain. Memory is faster to access than a file, a remote URL (usually), a database or any other external store of information you like. So if the act of using one of those external resources is significant then you may benefit from caching to increase performance.

Knuth once said that premature optimization is the root of all evil. Well, premature caching is the root of all headaches as far as I'm concerned. Don't solve a problem until you have a problem. Every decision you make comes at a cost that you'll pay to implement it now and pay again to change it later so the longer you can put off making a deicsion and changing your system the better.

So first identify that you actually have a problem and where it is. Profiling, logging and other forms of performance testing will help you here. I can't stress enough how important this step is. The number of times I've seen people "optimize" something that isn't a problem is staggering.

Ok, so you have a performance problem. Say your pages are running a query that takes a long time. If it's a read then you have a number of options:

  • Run the query as a separate process and put the result into a cache. All pages simply access the cache. You can update the cached version as often as is appropriate (once a day, once a week, one every 5 seconds, whatever is appropriate);
  • Cache transparently through your persistence provider, ORM or whatever. Of course this depends on what technology you're using. Hibernate and Ibatis for example support query result caching;
  • Have your pages run the query if the result isn't in the cache (or it's "stale", meaning it is calculated longer ago than the specified "age") and put it into the cache. This has concurrency problems if two (or more) separate processes all decide they need to update the result so you end up running the same (expensive) query eight times at once. You can handle this locking the cache but that creates another performance problem. You can also fall back to concurrency methods in your language (eg Java 5 concurrency APIs).

If it's an update (or updates take place that need to be reflected in your read cache) then it's a little more complicated because it's no good having an old value in the cache and a newer value in the database such that you then provide your pages with an inconsistent view of the data. But broadly speaking there are four approaches to this:

  • Update the cache and then queue a request to update the relevant store;
  • Write through caching: the cache provider may provide a mechanism to persist the update and block the caller until that change is made; and
  • Write-behind caching: same as write-through caching but it doesn't block the caller. The update happens asynchronously and separately; and
  • Persistence as a Service models: this assumes your caching mechanism supports some kind of observability (ie cache event listeners). Basically an entirely separate process--unknown to the caller--listens for cache updates and persists them as necessary.

Which of the above methodologies you choose will depend a lot on your requirements, what technologies you're using and a whole host of other factors (eg is clustering and failover support required?).

It's hard to be more specific than that and give you guidance on what to do without knowing much more detail about your problem (like whether or not you have a problem).

这篇关于什么是缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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