你如何选择Memcached,Redis和Varnish?

时间:2021-09-23 17:25:54

I am really very confused about Memcached, Redis and Varnish. I know they are used for caching, but I don't know how much they help, and how you know which one to use.

我对Memcached,Redis和Varnish非常困惑。我知道它们用于缓存,但我不知道它们有多大帮助,以及您如何知道使用哪一个。

And lastly, I would like know what you would use for a site with user-uploaded pictures and videos? If it helps I am using the Django framework.

最后,我想知道您将使用用户上传的图片和视频的网站?如果它有助于我使用Django框架。

2 个解决方案

#1


16  

Varnish is the odd one out, it's a server that runs as a reverse proxy in front of the real webserver (apache, nginx, etc.) and it stores the response of the server separately and could decide to serve it for a subsequent request without passing the request to the backend (the webserver), so simply it's like HTML caching.

Varnish是奇怪的,它是一个服务器,在真正的网络服务器(apache,nginx等)前面作为反向代理运行,它分别存储服务器的响应,并可以决定为后续请求提供服务将请求传递给后端(Web服务器),所以就像HTML缓存一样。

Memcached and redis are actually data storage servers, specifically key-value storage servers. In terms of python you could say it's one huge 'dictionary', you set values with a key and retrieve them by key. There's few differences between both of them, you can simply Google memcached vs redis.

Memcached和redis实际上是数据存储服务器,特别是键值存储服务器。就python而言,您可以说它是一个巨大的“字典”,您可以使用键设置值并按键检索它们。它们之间几乎没有什么区别,你可以简单地使用Google memcached vs redis。

#2


4  

I'm using Django and memcached, so I can tell how to use that one.
I have some data, almost never changing, that require a small amount of time (a few seconds) to be loaded.
Caching them will turn seconds into fractions of seconds, so loading time is reduced by a factor of 10.

我正在使用Django和memcached,所以我可以告诉如何使用那个。我有一些数据,几乎从不改变,需要加载少量时间(几秒钟)。缓存它们会将秒数变成几分之一秒,因此加载时间减少了10倍。

Using memcached is very simple:

使用memcached非常简单:

  1. Install and run memcached for your OS.

    为您的操作系统安装并运行memcached。

  2. Configure Django to use it as caching mechanism.
    In settings.py or equivalent set BACKEND option:
    'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache'

    配置Django将其用作缓存机制。在settings.py或等效设置中设置BACKEND选项:'BACKEND':'django.core.cache.backends.memcached.MemcachedCache'

  3. Store and retrieve data from memcached:

    存储和检索memcached中的数据:

    from django.core.cache import cache
    cache.set('myData',myData)
    # ... in another place:
    myData = cache.get('myData')
    if not myData:
         # re-calculate myData in case of a 'cache miss', then re-caching it.
         # myData = <calculations>
         cache.set('myData',myData)
    

#1


16  

Varnish is the odd one out, it's a server that runs as a reverse proxy in front of the real webserver (apache, nginx, etc.) and it stores the response of the server separately and could decide to serve it for a subsequent request without passing the request to the backend (the webserver), so simply it's like HTML caching.

Varnish是奇怪的,它是一个服务器,在真正的网络服务器(apache,nginx等)前面作为反向代理运行,它分别存储服务器的响应,并可以决定为后续请求提供服务将请求传递给后端(Web服务器),所以就像HTML缓存一样。

Memcached and redis are actually data storage servers, specifically key-value storage servers. In terms of python you could say it's one huge 'dictionary', you set values with a key and retrieve them by key. There's few differences between both of them, you can simply Google memcached vs redis.

Memcached和redis实际上是数据存储服务器,特别是键值存储服务器。就python而言,您可以说它是一个巨大的“字典”,您可以使用键设置值并按键检索它们。它们之间几乎没有什么区别,你可以简单地使用Google memcached vs redis。

#2


4  

I'm using Django and memcached, so I can tell how to use that one.
I have some data, almost never changing, that require a small amount of time (a few seconds) to be loaded.
Caching them will turn seconds into fractions of seconds, so loading time is reduced by a factor of 10.

我正在使用Django和memcached,所以我可以告诉如何使用那个。我有一些数据,几乎从不改变,需要加载少量时间(几秒钟)。缓存它们会将秒数变成几分之一秒,因此加载时间减少了10倍。

Using memcached is very simple:

使用memcached非常简单:

  1. Install and run memcached for your OS.

    为您的操作系统安装并运行memcached。

  2. Configure Django to use it as caching mechanism.
    In settings.py or equivalent set BACKEND option:
    'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache'

    配置Django将其用作缓存机制。在settings.py或等效设置中设置BACKEND选项:'BACKEND':'django.core.cache.backends.memcached.MemcachedCache'

  3. Store and retrieve data from memcached:

    存储和检索memcached中的数据:

    from django.core.cache import cache
    cache.set('myData',myData)
    # ... in another place:
    myData = cache.get('myData')
    if not myData:
         # re-calculate myData in case of a 'cache miss', then re-caching it.
         # myData = <calculations>
         cache.set('myData',myData)