Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

royshouvik
Copy link

@royshouvik royshouvik commented May 21, 2020

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

⚠️ Do not default to using this feature unless you need it. Getting and setting namespace keys requires a extra bit of round trip to memcache.

Example

await client.set('user1234', 'About user with id 1234', { namespace: 'bio' })
await client.set('user5678', 'Bio of  user 5678', { namespace: 'bio' })

await client.get('user1234', { namespace: 'bio' }) // returns 'About user with id 1234'

// Invalidate all keys in the 'bio' namespace
await client.invalidateNamespace('bio')

await client.get('user1234', { namespace: 'bio' }) // returns null

@royshouvik royshouvik force-pushed the feature/add-namespace-support branch from 04bc9f2 to 6e9c8de Compare May 21, 2020 07:17
@royshouvik royshouvik force-pushed the feature/add-namespace-support branch from 6e9c8de to fab9666 Compare May 21, 2020 07:24
Copy link
Owner

@victorquinn victorquinn left a 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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return _.toString(prefix) + '_' + key;
return _.toString(prefix) + ':' + key;

Copy link
Owner

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');
Copy link
Owner

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

  1. Add a limit on the length of a namespace as well (here at 20 characters)
  2. 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).

Copy link
Author

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.

Copy link

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

Copy link
Owner

@victorquinn victorquinn left a 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.

@royshouvik
Copy link
Author

Thanks for taking a look at it.

await client.set('firstName', 'Victor') // sets myNamespace:firstName

Maybe this isn't clear, but this feature is not about using a constant prefix to add for every keys.
A constant prefix can be easily managed in user land.

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.

@royshouvik
Copy link
Author

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.

Cool, will add them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for namespaces
3 participants