简体   繁体   English

1GB RAM上的memcached tomcat mysql

[英]memcached tomcat mysql on 1GB RAM

I am new to memcached and caching in general. 我是memcached和缓存的新手。 I have a java web application running on Ubuntu + Tomcat + MySQL on a VPS Server with 1GB of memory. 我有一个Java Web应用程序在具有1GB内存的VPS服务器上的Ubuntu + Tomcat + MySQL上运行。

Does it make sense to add a memcached layer with about 256MB for caching? 添加具有约256MB的内存缓存层进行缓存是否有意义? Will this be too much load on the server? 这会给服务器带来过多的负担吗? Which is more appropriate caching rendered html pages or database objects? 哪种方法更适合缓存呈现的html页面或数据库对象?

Please advise. 请指教。

If you're going to cache pages, don't use memcached, use Varnish . 如果要缓存页面,请不要使用memcached,而要使用Varnish However, there's a good chance that's not a great use of memory. 但是,很有可能不是大量使用内存。 Cacheing pages trades memory for computation and database work, but it does cost quite a lot of memory per page, so it's best for cases where the computation and database work needed to produce a single page amounts to a lot (or the pages are very small!). 缓存页面会为计算和数据库工作交换内存,但是每页确实要花费大量内存,因此,对于产生单个页面所需的计算和数据库工作量很大(或者页面很小)的情况,最好使用缓存!)。 Also, consider that page cacheing won't be effective, or even possible, if you want to use per-user customisation on your pages (eg showing the number of items in a shopping cart). 另外,如果您要在页面上使用按用户自定义(例如,显示购物车中的商品数量),请考虑页面缓存无效,甚至可能。 At least not without getting into some truly hairy shenanigans (edge-side includes, anyone?). 至少并非没有进入一些真正毛茸茸的恶作剧(边缘包括,有人吗?)。

If you're not going to cache pages, and your app is on a single machine, then there's no point using memcached or similar. 如果您不打算缓存页面,并且您的应用程序位于单台计算机上,那么使用memcached或类似内容就毫无意义。 The point of cache servers like that is to make the memory on one machine work as a cache for another - like how a file server shares a disk, they're essentially memory servers. 这样的缓存服务器的要点是使一台计算机上的内存充当另一台计算机的缓存-就像文件服务器共享磁盘的方式一样,它们实际上是内存服务器。 On a single machine, you might as well give all the memory to Java and cache objects on the heap. 在单台计算机上,您最好将所有内存分配给Java并在堆上缓存对象。

Are you using an object-relational mapper? 您是否正在使用对象关系映射器? If so, see if it has any support for a second-level cache. 如果是这样,请查看它是否支持二级缓存。 The big three implementations (Hibernate, OpenJPA, and EclipseLink) all support in-memory caches. 三大实现(Hibernate,OpenJPA和EclipseLink)均支持内存中的缓存。 They're likely to do a much better job than you would if you did the cacheing yourself. 与您自己进行缓存相比,它们可能会做得更好。

But, if you're not using a mapper, you have no choice but to do the cacheing yourself. 但是,如果您不使用映射器,则别无选择,只能自己进行缓存。 There are extension points in LinkedHashMap for building LRU caches, and then of course there's the people's favourite, SoftReference, in combination with a HashMap. LinkedHashMap中有用于构建LRU缓存的扩展点,然后当然还有人们最喜欢的SoftReference与HashMap结合使用。 Plus, there are probably cache implementations out there you could download and use - i'd be shocked if there wasn't something in the Apache Commons libraries. 另外,可能还有一些缓存实现可以下载和使用-如果Apache Commons库中没有任何东西,我会感到震惊。

memcached won't add any noticeable load on your server, but it will be memory your app can't use. memcached不会在您的服务器上添加任何明显的负载,但这将是您的应用无法使用的内存。 If you only plan to have a single app server for a while, you're better off using an in-JVM cache. 如果您仅打算暂时使用一台应用服务器,那么最好使用JVM中的缓存。

As far what to cache, the answer falls somewhere in the middle of the above. 至于缓存是什么 ,答案在上面的中间某处下降。 You don't want to cache exactly what's in your database and you certainly don't want to cache the final output. 您不想完全缓存数据库中的内容,当然也不想缓存最终输出。 You have a data model representation in your application that isn't exactly what's in the DB (eg a User object might be made up of multiple queries from a few different tables). 您的应用程序中的数据模型表示形式与数据库中的不完全相同(例如,一个User对象可能由来自几个不同表的多个查询组成)。 Cache that kind of thing as it's most reusable. 缓存最可重用的东西。

There's lots of info in the memcached site that should help you understand and get going with caching in general and memcached specifically. memcached站点中有很多信息,这些信息应该可以帮助您了解一般的缓存以及专门针对memcached的缓存。

It might make sense to do that, why don't try a smaller size like 64 MB and see how that goes. 这样做可能是有道理的,为什么不尝试使用较小的大小(例如64 MB),看看情况如何。 When you use more resources for the memcache, there is less for everything else. 当您将更多资源用于内存缓存时,其他所有资源都将减少。 You should try it and see what will give you the best performance. 您应该尝试一下,看看能为您带来最佳性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM