diff --git a/README.md b/README.md
index c5237a8..8c4d307 100644
--- a/README.md
+++ b/README.md
@@ -112,7 +112,7 @@
* SELECT index ✓
* SETBIT key offset value
* SETEX key seconds value ✓
- * SETNX key value
+ * SETNX key value ✓
* SETRANGE key offset value
* SHUTDOWN
* SINTER key [key ...]
diff --git a/index.html b/index.html
index e10bda7..1e9b053 100644
--- a/index.html
+++ b/index.html
@@ -464,6 +464,13 @@
Scenarios
assert_equal(l.get("foo"), "Hello")
},
+ "SETNX should set a key only if the key does not exists": function(){
+ l.setnx("foo", "Hello");
+ l.setnx("foo", "World");
+
+ assert_equal(l.get("foo"), "Hello");
+ },
+
});
diff --git a/lodis.coffee b/lodis.coffee
index 5be0da9..3c98ef7 100644
--- a/lodis.coffee
+++ b/lodis.coffee
@@ -343,3 +343,5 @@ class @Lodis
this.set(key, value)
this.expire(key, expire)
+ setnx: (key, value) -> this.set(key, value) if !this.exists(key)
+
diff --git a/lodis.js b/lodis.js
index 9a6511b..59d2100 100644
--- a/lodis.js
+++ b/lodis.js
@@ -536,6 +536,11 @@
this.set(key, value);
return this.expire(key, expire);
};
+ Lodis.prototype.setnx = function(key, value) {
+ if (!this.exists(key)) {
+ return this.set(key, value);
+ }
+ };
return Lodis;
})();
}).call(this);