首页 \ 问答 \ memcached:item在缓存中不可用(memcached: item is not available in cache)

memcached:item在缓存中不可用(memcached: item is not available in cache)

我有memcached的问题。 我使用带有模板'question_%d_%d'的字符串键存储值。 我记得:

STAT bytes 13307757 
STAT limit_maxbytes 134217728

但这是我的应用程序的日志:

2012-01-03 16:40:42,896 Get question for key question_4_1045: cache miss
2012-01-03 18:03:10,270 Get question for key question_4_1045: cache miss
2012-01-03 22:26:16,454 Get question for key question_4_1045: cache miss
2012-01-04 02:01:54,639 Get question for key question_4_1045: cache miss
2012-01-04 02:45:03,647 Get question for key question_4_1045: cache miss
2012-01-04 02:46:55,880 Get question for key question_4_1045: cache hit
2012-01-04 02:51:55,606 Get question for key question_4_1045: cache miss

所以我们可以看到,使用相同密钥的两个顺序调用导致两个缓存未命中,并且只从缓存中获取一次值。

为什么memcached从缓存中删除我的数据,即使有足够的空间? 有可能解决它吗?

我试图检查我的memcached日志文件(根据配置文件是/var/log/memcached.log),但它是空的。 谢谢!

UPD:Django缓存设置:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'TIMEOUT': 259200,
    }
}

CACHE_BACKEND = 'memcached://127.0.0.1:11211/'

吸气:

from django.core.cache import cache    
def get_question(level, random_num):
        key = 'question_' + unicode(level) + '_' + unicode(random_num)
        question = cache.get(key)
        if question is None:
            question = Question.objects.filter(level=level).order_by('id')[random_num]
            cache.set(key, question)
            log_message('Get question for key %s: cache miss' % key)
        else:
            log_message('Get question for key %s: cache hit' % key)
        return question

I have an issue with memcached. I store values using string key with template 'question_%d_%d'. I have enought memory:

STAT bytes 13307757 
STAT limit_maxbytes 134217728

but here is a log of my application:

2012-01-03 16:40:42,896 Get question for key question_4_1045: cache miss
2012-01-03 18:03:10,270 Get question for key question_4_1045: cache miss
2012-01-03 22:26:16,454 Get question for key question_4_1045: cache miss
2012-01-04 02:01:54,639 Get question for key question_4_1045: cache miss
2012-01-04 02:45:03,647 Get question for key question_4_1045: cache miss
2012-01-04 02:46:55,880 Get question for key question_4_1045: cache hit
2012-01-04 02:51:55,606 Get question for key question_4_1045: cache miss

so we can see, that two sequential calls with the same key lead to two cache misses, and only once value is fetched from cache.

Why memcached removes my data from cache, even if there is enough space? Is it possible to fix it?

I tried to check my memcached log file (according to config file it is /var/log/memcached.log), but it is empty. Thanks!

UPD: Django cache settings:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'TIMEOUT': 259200,
    }
}

CACHE_BACKEND = 'memcached://127.0.0.1:11211/'

getter:

from django.core.cache import cache    
def get_question(level, random_num):
        key = 'question_' + unicode(level) + '_' + unicode(random_num)
        question = cache.get(key)
        if question is None:
            question = Question.objects.filter(level=level).order_by('id')[random_num]
            cache.set(key, question)
            log_message('Get question for key %s: cache miss' % key)
        else:
            log_message('Get question for key %s: cache hit' % key)
        return question

原文:https://stackoverflow.com/questions/8724337
更新时间:2023-04-05 13:04

最满意答案

到目前为止,转换为秒是唯一的方法。


Converting to seconds is the only way so far.

相关问答

更多
  • 也许是这样的 $date = \DateInterval::createFromDateString('2 days 5 minutes'); function doSomeMagic(\DateInterval $date) { $interval = 'P' . ($date->y == 0 ? null : $date->y . 'Y') . ($date->m == 0 ? null : $date->m . 'M') . ($date->d ...
  • 从手册 间隔规格 格式以字母P开头,为“期间”。 每个持续时间由一个整数值表示,后跟一个周期指示符。 如果持续时间包含时间元素,则说明书的该部分前面加上字母T. From the manual Interval specification. The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designa ...
  • 您可以访问DateInterval类的公共属性并对其进行一些计算: $seconds = $d->s + ($d->i * 60) + ($d->h * 3600) + ($d->d * 86400) + ($d->m * 2592000); // and so on 但一旦我们进入月份,除非我们坚持任意定义一个月为2592000 secs (30天),否则会有±2天的差异。 您还可以使用DateTime对象中两个UNIX时间戳的差异(但最终会遇到日期小于1970年的问题): $seconds = $en ...
  • 深入了解Paul T. Rawkeen的答案 , DateTime::diff的问题在于它首先在计算之前将时区转换为UTC 。
  • 非常感谢。 它允许我这样做: public function callRuleCeilling($period) { $start = new \DateTime(); $month = 'January'; switch ($period) { case 'weekly': $timestampMonday = strtotime('last monday', strtotime('tomo ...
  • 请参阅, $interval是一个对象,而不是一些原始值。 在您的示例中,此间隔包括两年,零个月和零天。 当您查询其属性时,它不会自动转换为“以月为单位的间隔,以天为单位的间隔”等:它只返回它们的值。 这是非常正确的:例如,你应该考虑一个月间隔29天的间隔吗? 唯一的例外是$days属性(不是$d !),它实际上具有该间隔中天数的计算值。 它在文档中有很好的描述: $天 DateTime :: diff()计算中起始日期和结束日期之间的总天数 See, $interval is an object, not ...
  • 到目前为止,转换为秒是唯一的方法。 Converting to seconds is the only way so far.
  • 好吧,如果你在构造一个格式时查看格式的规范: Y年 M个月 D天 W周。 这些转换成天,所以不能与D结合。 H小时 M分钟 S秒 然后看看你有什么工作( http://php.net/manual/en/dateinterval.format.php ),看起来你会做的是: $dateInterval = new DateInterval( /* whatever */ ); $format = $dateInterval->format("P%yY%mM%dD%hH%iM%sS"); //P0Y0M5D0 ...
  • PHP文档中的注释只是部分正确。 到目前为止,我所阅读和实验过的所有内容似乎都表明DatePeriod 在使用结束日期时不适用于负DateIntervals。 也许有一些初步检查,在它做任何事情之前,最小值小于最大值,但我真的不确定为什么它不起作用。 但是,如果使用recurrences 构造函数而不是设置结束日期,它确实有效。 $dateRange = new DatePeriod($mostRecentSunday, $dateInterval, 3); // using 3 rather than 4 ...
  • 我建议存储DateInterval构造函数所用的间隔规范,如“P35D”,“P1DT24M39S”等。 我之所以选择这个: 这很简单。 如果您让用户选择特定的时间间隔,我假设您有一个表单,其中包含不同时间单位的不同输入。 从输入中创建其中一个字符串会非常简单,而不是在时间单位之间转换来存储int。 它相对紧凑。 没有像将它转换为一个时间单位并将其存储为int那样紧凑,但比序列化DateInterval对象和存储整个事物要紧凑得多。 我可能忽略了一个让其他东西成为更好选择的因素,但我主要是想分享我之前评论的推 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)