推荐一个内存数据库

发布于 2024-10-20 06:30:52 字数 124 浏览 7 评论 0原文

我想删除对 Web 应用程序上(几乎)每个请求加载的小数据块的 sql 依赖性。大多数数据都是键值/文档结构的,但也不排除关系型解决方案。数据不太大,所以我想将其保留在内存中以获得更高的可用性。

您会推荐什么解决方案?

I would like to remove sql dependency of small chunks of data that I load on (almost) each request on a web application. Most of the data is key-value/document structured, but a relational solution is not excluded. The data is not too big so I want to keep it in memory for higher availability.

What solution would you recommend?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

昔梦 2024-10-27 06:30:52

最简单且使用最广泛的内存键值存储是MemcacheD。介绍页面重申了您的要求:

Memcached 是一种内存中键值存储,用于存储来自数据库调用、API 调用或页面渲染结果的小块任意数据(字符串、对象)。

客户名单令人印象深刻。已经很久了。好的文档。它具有适用于几乎所有编程语言的 API。水平缩放非常简单。根据我的经验,Memcached 很好。

您可能还想查看 MemBase

The simplest and most widely used in-memory Key-value storage is MemcacheD. The introduction page re-iterates what you are asking for:

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

The client list is impressive. It's been for a long time. Good documentation. It has API for almost every programming language. Horizontal scaling is pretty simple. As my experience goes Memcached is good.

You may also want to look into MemBase.

-残月青衣踏尘吟 2024-10-27 06:30:52

Redis 非常适合此类数据。它还支持一些基本的数据结构并提供对它们的操作。

我最近转换了我的 Django 论坛应用程序,将其用于所有实时/跟踪数据 - 当您执行此类操作时不再有令人讨厌的感觉,真是太好了(SET 视图 = 视图 + 1和其他在每个页面视图上写入)与关系数据库。

以下是使用 Redis 存储用户活动跟踪所需数据的示例,包括在 Python 中保持最近查看的用户的有序集合为最新:

def seen_user(user, doing, item=None):
    """
    Stores what a User was doing when they were last seen and updates
    their last seen time in the active users sorted set.
    """
    last_seen = int(time.mktime(datetime.datetime.now().timetuple()))
    redis.zadd(ACTIVE_USERS, user.pk, last_seen)
    redis.setnx(USER_USERNAME % user.pk, user.username)
    redis.set(USER_LAST_SEEN % user.pk, last_seen)
    if item:
        doing = '%s <a href="%s">%s</a>' % (
            doing, item.get_absolute_url(), escape(str(item)))
    redis.set(USER_DOING % user.pk, doing)

Redis is perfect for this kind of data. It also supports some fundamental datastructures and provides operations on them.

I recently converted my Django forum app to use it for all real-time/tracking data - it's so good to no longer have the icky feeling you get when you do this kind of stuff (SET views = views + 1 and other writes on every page view) with a relational database.

Here's an example of using Redis to store data required for user activity tracking, including keeping an ordered set of last seen users up to date, in Python:

def seen_user(user, doing, item=None):
    """
    Stores what a User was doing when they were last seen and updates
    their last seen time in the active users sorted set.
    """
    last_seen = int(time.mktime(datetime.datetime.now().timetuple()))
    redis.zadd(ACTIVE_USERS, user.pk, last_seen)
    redis.setnx(USER_USERNAME % user.pk, user.username)
    redis.set(USER_LAST_SEEN % user.pk, last_seen)
    if item:
        doing = '%s <a href="%s">%s</a>' % (
            doing, item.get_absolute_url(), escape(str(item)))
    redis.set(USER_DOING % user.pk, doing)
只有一腔孤勇 2024-10-27 06:30:52

如果您不介意 sql 但希望将数据库保留在内存中,您可能需要查看 sqlite (请参阅 http://www.sqlite.org/inmemorydb.html)。

如果您不需要 sql 并且实际上只有键值对,为什么不将它们存储在映射/散列/关联数组中并完成它呢?

If you don't mind the sql but want to keep the db in memory, you might want to check out sqlite (see http://www.sqlite.org/inmemorydb.html).

If you don't want the sql and you really only have key-value pairs, why not just store them in a map / hash / associative array and be done with it?

红墙和绿瓦 2024-10-27 06:30:52

如果您最终需要内存数据库,H2 是一个非常好的选择。

If you end up needing an in-memory database, H2 is a very good option.

一身仙ぐ女味 2024-10-27 06:30:52

另外一个值得考虑的数据库:Berkeley DB。 Berkeley DB 允许您将数据库配置为内存中、磁盘上或两者。它支持键值 (NoSQL) 和 SQL API。 Berkeley DB 通常与 Web 应用程序结合使用,因为它是嵌入式的、易于部署(与您的应用程序一起部署)、高度可配置且非常可靠。有几个电子零售网站依赖 Berkeley DB 来提供其电子商务应用程序,其中包括 Amazon.com。

One more database to consider: Berkeley DB. Berkeley DB allows you to configure the database to be in-memory, on-disk or both. It supports both a key-value (NoSQL) and a SQL API. Berkeley DB is often used in combination with web applications because it's embedded, easily deployed (it deploys with your application), highly configurable and very reliable. There are several e-Retail web sites that rely on Berkeley DB for their e-Commerce applications, including Amazon.com.

拥有 2024-10-27 06:30:52

我不确定这是否是您正在寻找的,但您应该研究缓存框架(您现在使用的工具中可能包含该框架)。使用存储库模式,您需要获取数据,然后通过键检查缓存中是否有数据。如果你不这样做,你从数据库中获取它,如果你这样做,你从缓存中获取它。

这取决于您正在处理的数据类型,因此您可以决定将数据在缓存中保留多长时间。也许滑动超时是最好的,因为只要密钥不断被请求,您就会保留数据。这意味着如果缓存中有用户的数据,一旦用户离开,缓存中的数据就会过期。

I'm not sure this is what you are looking for but you should look into a caching framework (something that may be included in the tools you are using now). With a repository pattern you ask for the data, there you check if you have it in cache by key. I you don't, you fetch it from the database, if you do, you fetch it from the cache.

It will depend on what kind of data you are handling so it's up to you to decide how long to keep data in cache. Perhaps a sliding timeout is best as you'll keep the data as long as the key keeps being request. Which means if the cache has data for a user, once the user goes away, the data will expire from the cache.

美人骨 2024-10-27 06:30:52

你能分割这些数据吗?数据访问模式是否简单、稳定(不随业务需求的变化而变化)?这些数据有多重要(例如,会话上下文不太难恢复,而用户在设置页面上输入的某些首选项不应丢失)?

通常,如果您可以进行分片并且您的数据访问模式简单并且不会发生太大变化,那么您会选择 Redis。如果您正在寻找更可靠并支持更高级的数据访问模式的东西,Tarantool 是一个不错的选择。

Can you shard this data? Is data access pattern simple and stable (does not change with changing business requirements)? How critical is this data (session context, for example, is not too hard to restore, whereas some preferences a user has entered on a settings page should not be lost)?

Typically, provided you can shard and your data access patterns are simple and do not mutate too much, you choose Redis. If you look for something more reliable and supporting more advanced data access patterns, Tarantool is a good option.

失去的东西太少 2024-10-27 06:30:52

请检查一下:

http://www.mongodb.org/

它是一个非常好的 No-SQL 数据库具有对所有主要语言的驱动程序和支持。

Please do check out this :

http://www.mongodb.org/

Its a really good No-SQL database with drivers and support for all major languages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文