Will groupcache be the right cache for the page view use case?

blov · · 553 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m trying to find the unique post views for one of my web application which is similar to stackoverflow. I am following the accepted answer in this post [ <a href="http://meta.stackexchange.com/questions/36728/how-are-the-number-of-views-in-a-question-calculated" rel="nofollow">http://meta.stackexchange.com/questions/36728/how-are-the-number-of-views-in-a-question-calculated</a> ]. </p> <p>And I am using golang. For the cache I am planning to use groupcache [ <a href="https://github.com/golang/groupcache" rel="nofollow">https://github.com/golang/groupcache</a> ]. Will that be right fit for my use case? Could someone give some suggestions for this?</p> <hr/>**评论:**<br/><br/>gureggu: <pre><p>groupcache doesn&#39;t have time-based expiry, so I would say no. Use memcached or Redis, IMO.</p></pre>bradleyfalzon: <pre><p>Also, Redis will likely provide other primitives that maybe useful for your application (queues, pub/sub, different caches (LRU), autocomplete, more performant counters such has hyperloglog), as well as providing persistence, and more I&#39;m not thinking of.</p></pre>dineshappavoo: <pre><p>I had the same concern. But this post [ <a href="http://talks.soryy.com/2014/october/groupcache.slide#6" rel="nofollow">http://talks.soryy.com/2014/october/groupcache.slide#6</a> ]suggests that we can write our own TTL in groupcache. Again I totally agree with you. But I thought if there are advantages over others like, 1.we dont have to run separate servers [redis] for cache etc. we can try that. Do you have any comments on this?</p></pre>bradleyfalzon: <pre><p>That will effectively do what you want, but won&#39;t those keys still exist and consume memory? I would think you&#39;d need another list of used keys, or a method to walk all keys and delete old keys.</p></pre>dineshappavoo: <pre><p>That&#39;s right. It looks like LRU mechanism will remove the keys when they are not used. I think it is based on the memory size allocated for the cache. Here is the reference,</p> <p><a href="https://github.com/golang/groupcache/issues/42#issuecomment-77758650" rel="nofollow">https://github.com/golang/groupcache/issues/42#issuecomment-77758650</a></p> <p><strong><em>In my case, I needed expiration of about an hour for keys. What I did was add the timestamp of the next round hour to any key I&#39;m trying to get. Thus when an hour passes, this part of the key my app is requesting changes, and from Groupcache&#39;s point of view, I&#39;m asking for a new key. The old one will get evicted via the LRU mechanism as no one touches it anymore.</em></strong></p></pre>bradleyfalzon: <pre><p>So either, you&#39;ll allocate not enough memory and have active keys evicted, or you&#39;ll allocate too much memory. You&#39;ll need to monitor those levels to ensure you always maintain enough memory in the future.</p> <p><del>Does this data need to be persistent?</del></p> <p>Edit: OK, so you&#39;d use the GroupCache to <em>only</em> check whether the user&#39;s view has already been allocated to the posts total view count? Eg:</p> <pre><code>key = yyyymmddhh + ip + postID if cache.Set(key, someval) { // Set was successful, so key didn&#39;t exist // Update posts actual count db.Exec(&#34;UPDATE posts SET views = views + 1 WHERE postID = ?&#34;, postID) } else { // Set wasn&#39;t successful, key already existed // Don&#39;t update post as the user probably refreshed the page } </code></pre> <p>Is that how you were planning to implement this?</p> <p>Redis still feels like a better option given the additional use cases and TTL. For a redis implementation, I&#39;d probably look at: <a href="http://redis.io/topics/data-types#sets" rel="nofollow">http://redis.io/topics/data-types#sets</a> - setting the key to be the hour with an hour expiry if the set was successful (or something along those lines, there maybe a more efficient method via transactions, either way, there&#39;s no SETEX for sets it seems).</p></pre>dineshappavoo: <pre><p>That&#39;s right. Looks like groupcache does not provide an API to get whether the key is available in cache. It is a must requirement for this use case. Thanks for the suggestion. I gave a try with redis.</p> <p>Here is the snippet. <a href="https://gist.github.com/dineshappavoo/a07fe5d8d74e9d182c7882853e944240" rel="nofollow">https://gist.github.com/dineshappavoo/a07fe5d8d74e9d182c7882853e944240</a> . I appreciate your help. </p></pre>gureggu: <pre><p>Consider that groupcache doesn&#39;t have the concept of setting data, just getting data from somewhere else (or computing it, etc) and caching it. You also can&#39;t tell whether something came from the on-memory cache or the getter function or another host. I don&#39;t think you could implement your page count thing with it alone.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

553 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传