-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add namespace support #67
base: main
Are you sure you want to change the base?
add namespace support #67
Conversation
04bc9f2
to
6e9c8de
Compare
6e9c8de
to
fab9666
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So overall I like the concept of namespace, but think rather than having to pass it to every set
and get
that it'd be better in the constructor, then it'd never have to be specified again.
This way you could set it once for one instance of this library and not have to mess with it again. Having to pass it to every call mostly negates the benefit of the feature in my opinion.
At that point, it'd be far easier to just call it every time with:
const MemcachePlus = require('memcache-plus')
const client = new MemcachePlus()
await client.set(`${ns}:firstName`, 'Victor`)
const firstName = await client.get(`${ns}:firstName`)
console.log(`Hello ${firstName}`)
than to use your namespace feature as written:
const MemcachePlus = require('memcache-plus')
const client = new MemcachePlus()
await client.set('firstName', 'Victor', { namespace: ns })
const firstName = await client.get(`firstName`, { namespace: ns })
console.log(`Hello ${firstName}`)
instead I'd much prefer :
const MemcachePlus = require('memcache-plus')
const ns = 'myNamespace'
const client = new MemcachePlus({ namespace: ns })
await client.set('firstName', 'Victor') // sets myNamespace:firstName
const firstName = await client.get(`firstName`) // gets myNamespace:firstName
console.log(`Hello ${firstName}`)
// return a curried funtion that expects the key | ||
prefix = key; | ||
return function(key) { | ||
return _.toString(prefix) + '_' + key; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return _.toString(prefix) + '_' + key; | |
return _.toString(prefix) + ':' + key; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's please keep with the colon as the separator for namespaces as suggested by the Memcached article you linked
That's also the way I've always seen it done in production
@@ -19,6 +19,23 @@ function validateKey(key, operation) { | |||
misc.assert(key.length < 250, 'Key must be less than 250 characters long'); | |||
} | |||
|
|||
function validateNamespaceKey(key, operation) { | |||
validateKey(key, operation); | |||
misc.assert(key.length < 230, 'Key must be less than 230 characters long when using namespaces'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it's good that you're checking for the namespace key to be shorter, but this without a check on the length of namespace itself isn't super useful. There is nothing preventing me from having a key
that is 229 characters and a namespace
that is also 229 characters and being far past the 250 key limit set by Memcached core.
I'd much prefer to either
- Add a limit on the length of a namespace as well (here at 20 characters)
- Just run
validateKey()
after appending the namespace with the key
I favor 2. because it doesn't establish an arbitrary limit on namespace (you've chosen 20 and that seems rather arbitrary).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I wrote in my other comment, I think the idea of this feature was misunderstood.
It doesn't matter what the length of the name of the namespace is. The prefix is always going to be a unix timestamp which is around 13 characters. The key which uses namespace needs to be at most (250-13) characters long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great works for adding the name spacing feature!
https://www.npmjs.com/package/memcached-lite has a similar feature named keyPrefix
.
I use it with some prefix string longer than 20 characters.
In my case, the prefix would include some from below:
require("./package.json").name
- this works when a memcached server is shared by multiple apps.require("./package.json").version
- this works when updating the app which has a difference schema than before__filename.split("/").pop()
- this works to separate namespaces for each files.+new Date()
as @royshouvik mentioned
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, you only implemented it for get()
and set()
-- it needs to be added for all of the other methods to be useful. Otherwise you're calling get()
and set()
with it but unable to delete
, incr
, decr
, etc.
Thanks for taking a look at it.
Maybe this isn't clear, but this feature is not about using a constant prefix to add for every keys. On the other hand, this is about invalidating n number of keys, where we do not know the keys but we want to invalidate all of them as a group. |
Cool, will add them. |
This PR adds support for specifying optional namespace. This is useful in scenarios where we want to invalidate a group of keys but it's not possible/easy to know the name of the keys. Fixes #66
Example