-
Notifications
You must be signed in to change notification settings - Fork 177
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
pymemcache.exceptions.MemcacheUnknownCommandError: b'cas' #233
Comments
It seems that you have not defined the variable However, you will then run into either of the following errors: TypeError: can't concat int to bytes
# or
TypeError: can't concat str to bytes So just remember to prepend convert the character to bytes by simply prepending the character with a |
thank you @Dansyuqri |
@Dansyuqri unfortunately, that is not the correct answer to this question. I was getting the same error, and after much frustration figured out what causes it. If the key val, cas = client.gets('key')
client.cas('key', 'val', cas) I have no idea why this happens. It is not mentioned in the docs, and does not happen with other python libraries for memcached. You need to ensure the key is already in the cache, possibly by setting it to client.set('key', None)
val, cas = client.gets('key')
client.cas('key', 'val', cas) Hope this might be useful to someone else. If this is intended behaviour (seems like pretty weird behaviour and a very obscure error message for that to be the case), then I would suggest it is documented. Otherwise I would suggest this issue be reopened as a bug (@jogo). |
I'll reopen this for reinvestigation. I agree we'd at least want to update the documentation if this is intended behavior (I'm not sure if it is). |
I tried out these commands locally and was able to reproduce the error:
That is with a freshly started There are two possible fixes. The first is that we have the |
Just to be clear, @Dansyuqri already explained this in his comment, I'm just providing some alternatives to get a better error message when users encounter this issue. |
The
I think the right thing to do is to validate that parameter according to that input specification and raise Further, I see that
That argues against the Knowing all this, we can tweak @cgordon's example to become this if we want CAS behavior: >>> val, cas = c.gets('key', default_cas='1')
>>> c.cas('key', 'value', cas) ... and without the |
There are a few things I don’t quite get about this. First, if this is indeed what @Dansyuqri meant in his answer, then it wasn’t particularly clear (at least to me). He said:
However, the variable Second, why does this happen in the first place anyway? I am not an expert on these matters, but I feel that a valid cas value should be returned from Third, the proposed solution of supplying val, cas = client.gets('key')
ret = client.cas('key', 'val', b'0')
print(ret)
>>> None
print(mc.get('key'))
>>> None It seems to me a valid use case to want to set a (perhaps previously nonexistent) key to a particular value with a single |
I made a dumb remark about using First, I agree with @jamesoliverh that the current behavior is not user-friendly (at best), but it isn't inconsistent with the way the memcache protocol works. In the protocol, if you send a The goal of the
In this example, client A is attempting to increment the value of 'k' by one, and it would like to avoid race conditions with other writers, so it is using the So, if we returned a default value of The correct idiom for doing what client A is trying to do in my example above is this:
This is a really long argument for what @jparise was already proposing, which is that we should just raise a @jamesoliverh If it isn't completely clear why the code snippet above is the correct way to use |
@cgordon I also had a read of the memcached specification this morning and completely agree with your comment. I also agree that your example idiom using |
With #304, we now raise |
What other improvements should we consider making before we close this out? Some ideas:
|
My If we don't want to pursue any other improvements in this area, we can close this issue. |
I'm alright with closing it. |
from pymemcache.client.base import Client
client = Client(('localhost', 11211))
while True:
result, cas = client.gets('key')
if result is None:
result = "1"
else:
result += 1
if client.cas('key', result, cas):
break
Traceback (most recent call last):
File "zzz1.py", line 32, in
set = client.cas(key, result, cas)
File "/usr/local/lib/python3.6/site-packages/pymemcache/client/base.py", line 437, in cas
return self._store_cmd(b'cas', {key: value}, expire, noreply, cas)[key]
File "/usr/local/lib/python3.6/site-packages/pymemcache/client/base.py", line 842, in _store_cmd
self._raise_errors(line, name)
File "/usr/local/lib/python3.6/site-packages/pymemcache/client/base.py", line 732, in _raise_errors
raise MemcacheUnknownCommandError(name)
pymemcache.exceptions.MemcacheUnknownCommandError: b'cas'
The text was updated successfully, but these errors were encountered: