是否有标准的第三方Python缓存类?

时间:2022-09-11 21:47:02

I'm working on a client class which needs to load data from a networked database. It's been suggested that adding a standard caching service to the client could improve it's performance.

我正在研究一个需要从网络数据库加载数据的客户端类。有人建议向客户端添加标准缓存服务可以提高它的性能。

I'd dearly like not to have to build my own caching class - it's well known that these provide common points of failure. It would be far better to use a class that somebody else has developed rather than spend a huge amount of my own time debugging a home-made caching system.

我非常希望不必建立自己的缓存类 - 众所周知,这些提供了常见的失败点。使用其他人开发的类而不是花费大量时间来调试自制的缓存系统会好得多。

Java developers have this: http://ehcache.sourceforge.net/

Java开发人员有这个:http://ehcache.sourceforge.net/

It's a general purpose high-performance caching class which can support all kinds of storage. It's got options for time-based expiry and other methods for garbage-collecting. It looks really good. Unfortunately I cannot find anything this good for Python.

它是一个通用的高性能缓存类,可以支持各种存储。它有基于时间的到期和其他垃圾收集方法的选项。它看起来非常好。不幸的是,我找不到任何对Python有用的东西。

So, can somebody suggest a cache-class that's ready for me to use. My wish-list is:

那么,有人可以建议一个可供我使用的缓存类。我的愿望清单是:

  • Ability to limit the number of objects in the cache.
  • 能够限制缓存中的对象数量。

  • Ability to limit the maximum age of objects in the cache.
  • 能够限制缓存中对象的最大年龄。

  • LRU object expirey
  • LRU对象expirey

  • Ability to select multiple forms of storage ( e.g. memory, disk )
  • 能够选择多种形式的存储(例如内存,磁盘)

  • Well debugged, well maintained, in use by at least one well-known application.
  • 经过良好调试,维护良好,至少由一个众所周知的应用程序使用。

  • Good performance.

So, any suggestions?

那么,有什么建议吗?

UPDATE: I'm looking for LOCAL caching of objects. The server which I connect to is already heavily cached. Memcached is not appropriate because it requires an additional network traffic between the Windows client and the server.

更新:我正在寻找对象的LOCAL缓存。我连接的服务器已经被高度缓存。 Memcached不合适,因为它需要Windows客户端和服务器之间的额外网络流量。

1 个解决方案

#1


I'd recommend using memcached and using cmemcache to access it. You can't necessarily limit the number of objects in the cache, but you can set an expiration time and limit the amount of memory it uses. And memcached is used by a lot of big names. In fact, I'd call it kind of the industry standard.

我建议使用memcached并使用cmemcache来访问它。您不一定要限制缓存中的对象数,但可以设置到期时间并限制它使用的内存量。 memcached被很多大牌使用。事实上,我称之为行业标准。

UPDATE:

I'm looking for LOCAL caching of objects.

我正在寻找对象的LOCAL缓存。

You can run memcached locally and access it via localhost. I've done this a few times.

您可以在本地运行memcached并通过localhost访问它。我做了几次。

Other than that, the only solution that I can think of is django's caching system. It offers several backends and some other configuration options. But that may be a little bit heavyweight if you're not using django.

除此之外,我能想到的唯一解决方案是django的缓存系统。它提供了几个后端和一些其他配置选项。但如果你不使用django,那可能会有点重量级。

UPDATE 2: I suppose as a last resort, you can also use jython and access the java caching system. This may be a little difficult to do if you've already got clients using CPython though.

更新2:我想作为最后的手段,你也可以使用jython并访问java缓存系统。如果您已经让客户使用CPython,这可能有点难以做到。

UPDATE 3: It's probably a bit late to be of use to you, but a previous employer of mine used ZODB for this kind of thing. It's an actual database, but its read performance is fast enough to make it useful for caching.

更新3:对你有用可能有点晚了,但我以前的雇主使用ZODB来做这件事。它是一个实际的数据库,但其读取性能足够快,使其对缓存很有用。

#1


I'd recommend using memcached and using cmemcache to access it. You can't necessarily limit the number of objects in the cache, but you can set an expiration time and limit the amount of memory it uses. And memcached is used by a lot of big names. In fact, I'd call it kind of the industry standard.

我建议使用memcached并使用cmemcache来访问它。您不一定要限制缓存中的对象数,但可以设置到期时间并限制它使用的内存量。 memcached被很多大牌使用。事实上,我称之为行业标准。

UPDATE:

I'm looking for LOCAL caching of objects.

我正在寻找对象的LOCAL缓存。

You can run memcached locally and access it via localhost. I've done this a few times.

您可以在本地运行memcached并通过localhost访问它。我做了几次。

Other than that, the only solution that I can think of is django's caching system. It offers several backends and some other configuration options. But that may be a little bit heavyweight if you're not using django.

除此之外,我能想到的唯一解决方案是django的缓存系统。它提供了几个后端和一些其他配置选项。但如果你不使用django,那可能会有点重量级。

UPDATE 2: I suppose as a last resort, you can also use jython and access the java caching system. This may be a little difficult to do if you've already got clients using CPython though.

更新2:我想作为最后的手段,你也可以使用jython并访问java缓存系统。如果您已经让客户使用CPython,这可能有点难以做到。

UPDATE 3: It's probably a bit late to be of use to you, but a previous employer of mine used ZODB for this kind of thing. It's an actual database, but its read performance is fast enough to make it useful for caching.

更新3:对你有用可能有点晚了,但我以前的雇主使用ZODB来做这件事。它是一个实际的数据库,但其读取性能足够快,使其对缓存很有用。