首页 \ 问答 \ memcached可以用于锁定吗?(Can memcached be used for locking?)

memcached可以用于锁定吗?(Can memcached be used for locking?)

memcached可用于缓存静态数据,减少数据库查找,通常用于memcached.get(id)memcached.set(id)

但是将它用于锁定机制是否可以? memcache.setmemcached.get是否总是提供数据(如果存在),或者如果请求花费太多时间它会返回None吗?

我想避免并发访问由id标识的特定资源,我使用此逻辑:

def access(id):
    if memcache.get(id):
        return access
    else:
        memcache.set(id)
        return true

如果任何用户试图访问该资源,如果memcache.get(id) = username返回一个值,我们拒绝访问,否则我们执行memcache.set(id) = username以停止后续访问并允许当前用户访问。

这样使用memcached可以吗? 将set并实际提供数据,如果它可用,无论花费多少时间,或者它在最短的时间内从我找到的任何内容中获得最佳结果(例如: 保证的memcached锁 )到目前为止属于前一类和可能无法锁定以为它可能在99%的时间内起作用。

任何人都可以澄清,是否有其他锁定机制?


memcached can be used for a caching static data which reduces database lookup and typically does memcached.get(id) and memcached.set(id).

However is it fine to use this for locking mechanisms? Does memcache.set and memcached.get always give the data if it is present or will it just return None if the request is taking too much time?

I want to avoid concurrent access to a particular resource identified by a id and I use this logic:

def access(id):
    if memcache.get(id):
        return access
    else:
        memcache.set(id)
        return true

If any user tries to access that resource, if memcache.get(id) = username returns a value we decline the access else we do memcache.set(id) = username to stop subsequent access and allow access for the current user.

Is it fine to using memcached like this? Will set and get actually give the data if its available regardless of the time it takes or does it give best possible result in the least possible time from whatever I have found (for example: Guaranteed memcached lock) so far is of the former category and might not work for locking thought it might work 99% of the time.

Can anyone clarify and if there are alternative locking mechanisms?


原文:https://stackoverflow.com/questions/37458587
更新时间:2022-12-23 14:12

最满意答案

另一个网站很可能也会使用来自Google服务器的相同js文件,因此该文件已被您的浏览器缓存,并且无需再为您的网站下载该文件。

看看这篇文章的其他一些好处。


There is a good chance another site will also be using the same js files from Google's servers, so the file will already be cached by your browser and it won't need to download it again for your site.

Check out this article for some of the other benefits as well.

相关问答

更多
  • 优点:它可能已经缓存在用户的系统上。 Google有很大的管道。 你不支付带宽。 缺点:您现在有两种不同的方式让您的网站无法使用:服务器上的服务中断或Google服务器上的服务中断。 Pros: It may already be cached on the user's system. Google has big pipes. You don't pay for the bandwidth. Cons: You now have two different ways for your site to b ...
  • 尝试这个。 用此代码替换您的代码。 这将处理所有。 /************* ENQUEUE JS *************************/ /* pull jquery from google's CDN. If it's not available, grab the local copy. */ $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'; // the URL to check ...
  • 这里可能会出现两个不同的问题。 因为您每次都在调用googleMaps()函数,所以您正在创建一个新的对象实例,用于跟踪google maps api何时加载。 您需要提取在googleMaps函数之外加载api的部分(或使用共享变量)。 即使您保持loadAPI功能,您仍然可能遇到竞争条件,其中加载谷歌地图库的ajax请求可以被触发两次。 您看到该错误的原因是,当您检查loadAPI时,如果Google对象可用,则第一个ajax请求实际上尚未设置它。 您可以使用Promise API来处理这样的问题。 我 ...
  • 另一个网站很可能也会使用来自Google服务器的相同js文件,因此该文件已被您的浏览器缓存,并且无需再为您的网站下载该文件。 看看这篇文章的其他一些好处。 There is a good chance another site will also be using the same js files from Google's servers, so the file will already be cached by your browser and it won't need to download i ...
  • 1.确保map-canvas在CSS中设置了width 。 2.你什么时候试着调用google.maps.event.trigger(map,'resize'); 作为geocodezip建议? 您需要在任何map-canvas div的尺寸更改( calculateHeight )或者对地图div可见性的任何更改之后调用它。 如果map-canvas div或它的父级或它的父级的父级(任何前任)在某些时候display:none ,则地图视图将无法正确初始化,您将只看到灰色地图。 还要将map resiz ...
  • 使用loader api的主要优点是,在进行初始下载时,您将阻止浏览器阻止。 浏览器一次只能下载2到10个东西,所以如果有阻塞,它会给用户带来糟糕的体验 Steve Souders和雅虎! 卓越绩效团队已经对此进行了大量研究,以获得更快的网站。 尼克扎卡斯(JavaScript大师)在博客上发表了关于在这里使用史蒂夫的想法 the main advantage for using the loader api is that you will prevent blocking by the browser ...
  • 如果您正在注入,则可能更容易在不使用谷歌加载器的情况下请求脚本: (function() { var script = document.createElement("script"); script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"; script.onload = script.onreadystatechange = function(){ /* your callba ...
  • 有两种方法可以使用Ajax Libraries API。 首先,您可以使用Google来托管您的jQuery文件: 其次,您可以使用它来执行jQuery的异步加载,这就是您所指的。 如果你这样做,模式是: