带有EX和NX的Ruby REDIS(Ruby REDIS with EX AND NX)
我试图在我们的rails应用程序服务器中实现锁定。
如果我想永远获得一个锁,REDIS.setnx工作正常。 但我希望获得有效期限的锁定,基本上我希望锁定在一定时间后过期,以便锁定可以再次获得。
从REDIS的设置文档中,我发现它是可能的。 https://redis.io/commands/set “命令SET resource-name anystring NX EX max-lock-time是一种使用Redis实现锁定系统的简单方法。”
如何在ruby中实现这一点。 命令:
REDIS = Redis.new(host: ENV['REDIS_HOST'], port: ENV['REDIS_PORT']) REDIS.set "key", "value", "nx", "ex", 3600
抛出错误:
ArgumentError: wrong number of arguments (given 5, expected 2..3)
还有另一种方法,但需要两次REDIS调用。
if(REDIS.setnx "key", "value") REDIS.setex "key", 3600, "value" end
该方法不是优选的。 我正在寻找一种方法来获得REDIS锁定与红宝石单一REDIS调用。 基本上“SET资源名称anystring NX EX max-lock-time”等效于ruby。
谢谢,
Anshul
I am trying to implement a locking in our rails application server.
REDIS.setnx works fine if I want to acquire a lock forever. But I want to acquire lock with expiry, basically I want the lock to expire after certain duration so that lock will be free to be acquired again.
From REDIS's set documentation, I see it is possible. https://redis.io/commands/set "The command SET resource-name anystring NX EX max-lock-time is a simple way to implement a locking system with Redis."
How to implement this in ruby. Command :
REDIS = Redis.new(host: ENV['REDIS_HOST'], port: ENV['REDIS_PORT']) REDIS.set "key", "value", "nx", "ex", 3600
throws error:
ArgumentError: wrong number of arguments (given 5, expected 2..3)
There is another way to do that, but it requires two REDIS calls.
if(REDIS.setnx "key", "value") REDIS.setex "key", 3600, "value" end
This method is not preferred. I am looking to a way to acquire REDIS lock with single REDIS call in ruby. Basically "SET resource-name anystring NX EX max-lock-time" equivalent in ruby.
Thanks,
Anshul
原文:https://stackoverflow.com/questions/48318207
最满意答案
你可以使用这样的东西:
@Service public class MyPropertiesServ implements InitializingBean { @Autowired protected ApplicationContext context; private Properties properties; private static MyPropertiesServ singleInst; protected MyPropertiesServ () { properties = new Properties(); singleInst= this; } @Override public void afterPropertiesSet() throws Exception { properties.load(context.getResource("classpath:file.properties").getInputStream()); } public static String getStringProperty(String key) { return singleInst.properties.getProperty(key); } }
另一个例子: http : //forum.spring.io/forum/spring-projects/container/35703-initialising-static-property-of-class-through-injection-on-startup
You could use something like this:
@Service public class MyPropertiesServ implements InitializingBean { @Autowired protected ApplicationContext context; private Properties properties; private static MyPropertiesServ singleInst; protected MyPropertiesServ () { properties = new Properties(); singleInst= this; } @Override public void afterPropertiesSet() throws Exception { properties.load(context.getResource("classpath:file.properties").getInputStream()); } public static String getStringProperty(String key) { return singleInst.properties.getProperty(key); } }
Another example: http://forum.spring.io/forum/spring-projects/container/35703-initialising-static-property-of-class-through-injection-on-startup
相关问答
更多-
是不是有一款什么手机软件,可以很自然的拉长腿部?不是美图秀秀,有知道的不?[2022-10-20]
spring -
春天的英语怎么读?[2022-05-21]
spring -
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
最后,我可以解决我的问题,部分...... 我擦掉了 modelAndView.addObject("assetsFolder", this.assetsFolder); 然后,我添加了我的jsp页面 <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%> ....
-
您可以通过告知您希望附加可下载文件的响应来实现此目的。 然后,您可以简单地编写您想要下载的内容。 这是一个例子: @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/myredirect", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) public void downloadFile(HttpServletResponse res ...
-
我怀疑您正在更改本地代码中的资源,但不是在运行应用程序的Web容器中。 你能尝试从实际运行的webapp中改变js吗? I suspect you are changing the resources in your local code but not within the web container that is running your app. Can you try altering the js from within the webapp that is actually running?
-
你没有其他选择。 首先请注意,您的配置基于Spring的Resources抽象 在Spring MVC中,位置内的前缀指示查找策略,有四种类型 classpath: - resources loaded from the classpath. file: - resources loaded as a URL, from the filesystem. http: - resources loaded as a URL. (none) - depends on the underlying `Applica ...
-
你可以使用这样的东西: @Service public class MyPropertiesServ implements InitializingBean { @Autowired protected ApplicationContext context; private Properties properties; private static MyPropertiesServ singleInst; protected MyPropertiesServ () { properties = ...
-
此问题已修复。 我通过做一些修改来解决它,以简化mvc,security和web.xml配置。 它似乎在某种程度上陷入了春季安全问题。 This issue is fixed. I resolved it by making a few modifications to simplify the mvc, security and web.xml configurations. It seemed to be getting caught up in spring security somehow.