Does setting a memcached key that already exists refresh the expiration time? - memcached

Let's say I have the following code:
Memcached->set('key', 'value', 60); (expire in one minute)
while (1) {
sleep 1 second;
data = Memcached->get('key');
// update data
Memcached->set('key', data, 60);
}
After 60 iterations of the loop, will the key expire and when reading it I'll get a NULL? Or will the continuous setting keep pushing the expiration time each time to 1 minute after the last Set?
The documentation mentions this, and I've tested this in different contexts and I'm pretty sure I got different results.

Ok, found my answer by experimentation in the end...
It turns out "Set" does extend the expiration, it's basically the same as deleting the item and Setting it again with a new expiration.
However, Increment doesn't extend the expiration. If you increment a key, it keeps the original expiration time it had when you Set it in the first place.

If you simply want to extend the expiration time for a particular key instead of essentially resetting the data each time, you can just use Memcached::touch
With the caveat that you must have binary protocol enabled according to the comment on the above page.
$memcached = new Memcached();
$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$memcached->touch('key', 120);

The set doesn't care whatsoever about what may have been there, and can't assume that it even came from the same application.
What all did you test and what kinds of results did you get? Memcached never guarantees to return a value, so if you saw it go missing, it's quite possible to construct a test that would lose that regardless of expiration.

The best documentation source is the Memcached protocol description
First, the client sends a command line which looks like this:
<command name> <key> <flags> <exptime> <bytes> [noreply]\r\n
- <command name> is "set", "add", "replace", "append" or "prepend"
As you may see, each of the commands above have the exptime field which is mandatory.
So, yes - it will update expiration time. Moreover, memcached creates new item with its own key / flags / expiration / value and replaces the old one with it.

If your goal is to simply extends the expiration time, use the command touch, that was created to set a new expiration time for a key.
See https://manned.org/memctouch or http://docs.libmemcached.org/bin/memtouch.html
Debian package: libmemcached-tools
From the shell: man memctouch
other distros use "memtouch" as the name of the command line tool
+1 Link from memcached protocol, as a manual reference:
https://github.com/memcached/memcached/blob/master/doc/protocol.txt#L318
Example:
memctouch --servers=localhost --expire=60 $key
Where $key is your 'key', this will set the expire time to 60 seconds, as in your example, but without having to make a "get" AND re-set the key. What if your 'key' is not set yet and your 'get' doesn't return some data?

Related

patch endpoint naming and realisation

We have rest resource
/tasks/{task-type}
and only GET methods available.
GET /tasks/{task-type}
GET /tasks/{task-type}/{id}
Task entity contains meta info like created, finished, status, ref key and try counts for scheduled tasks.
Now we faced with problem, when task may contains incorrect data and its execution always failed.
Due to scheduler invoked tasks every 5 min there are a lot of errors in logs and largest try counts around 500k. The solution i found is to limit try_count to five (for example). And now we need way to manual discard try-count to zero. So i found two solutions:
1.
PATCH /tasks/{task-type}/{id}/discard-try-count - no response body
This solution look pretty simple, but violates the REST convention, because we use action(verb) in naming. But if we need to change other fields, then we will make a lot of endpoints in this style.
2a.
PATCH /tasks/{task-type}/{id}
body:
{
"tryCounts": int
}
This looks like REST want to see it and we can easy add new fields to modify, but now client can set any value for tryCount.
2b
PATCH /tasks/{task-type}/{id}
body:
{
"tryCounts": int // validate that try count can be only zero
}
Differs from the previous one by the presence of validation.
This looks like the most reliable solution. Is it really the best fit?
The non-verb convention is not a standard, you can violate it if you want to, though it can be worked around with very simple stuff, just convert the verb into a noun and you will be ok, something like:
POST /tasks/{task-type}/{id}/try-count-discarding
Another way is setting the try count to zero:
PUT /tasks/{task-type}/{id}/try-count 0
Yet another solution is combining the two, which I like the most:
PATCH /tasks/{task-type}/{id}/try-count {"op": "reset"}
Or another variant:
PATCH /tasks/{task-type}/{id} {"op": "discard-try-count"}

Nightbot command - !uptime combined with a different command, is it possible?

I don't know if I'm posting to the right place or what but I was wondering if someone could help me with a Nightbot command I want to make.
I have the !uptime command and I have a !rage command which only retrieves one value. Now I'd like to combine the two into a command that would retrieve 5 or 6 different values (stages of rage in this situation) depending on what value !uptime would retrieve. So basically if I have been streaming for an hour !rage would say minimum but if for 5 hours it would say critical or something.
How is this possible? Someone pls help
Go to https://nightbot.tv/commands/custom and simply add these two commands
command : message : alias
!hour : $(eval var index = $(1); const options=["min","avg","max","babyRage"]; options[index] )
!rage : $(urlfetch https://beta.decapi.me/twitch/uptime/itsashawe) : !hour
NOTE: Remember to use your channel name instead of mine (itsashawe). Make sure you add !hour as alias in the !rage command.
P.S.
I've used an api to make it simpler. The rage command calls the hour command which gets the hours, minutes and seconds. The rage command uses $(1) i.e. the first argument i.e. the hour and uses it as an index to get values from options which you can change according to your use.

Redis Hashes store with new line key value

I want to store data in Redis Hashes. Data is as below (Key = Value):
30.2.25=REF_IP
30.2.24=MY_HOST_IP
30.2.32=PEER_IP
30.2.32=IM_USER_MY_HOST
30.2.2=23992
Easy way to store this info in redis is below :
hmset info 30.2.25 REF_IP 30.2.24 MY_HOST_IP 30.2.32 PEER_IP 30.2.32 IM_USER_MY_HOST 30.2.2 23992
Considering I have 1000's key value and want to change few (actually so many) values in one go so searching and editing value in above command is too painful.
i want some way to execute command in below manner, that is nice formatted command with new line after every key value :
hmset info
30.2.25 REF_IP
30.2.24 MY_HOST_IP
30.2.32 PEER_IP
30.2.32 IM_USER_MY_HOST
30.2.2 23992
Is it possible to do so ?
Currently when i copy above formatted command and paste, it ignore test after new line and giving below error which is obvious because argument is wrong due to new line.
hmset info
(error) ERR wrong number of arguments for 'hmset' command
Can anyone help please. Thanks.
Assuming you are talking about using redis-cli, there is no way to support this at the moment. There is an open issue for this. See https://github.com/antirez/redis/issues/3474
As per Redis 4.0.0, HMSET is considered deprecated. You should use HSET instead. https://redis.io/commands/hset
You can use a transaction if you want to ensure all HSETs are done at the same time, and still enter them one line at a time.
MULTI
HSET info 30.2.25 REF_IP
HSET info 30.2.24 MY_HOST_IP
...
EXEC
The commands will be sent to the server one line at a time, but they are queued and only executed at the EXEC command.
You may use another client, say in Python, and then do something fancier as well to condense your field-value hsets into one command.

Movesense, setting the system time

I am trying to set the system time in Movesense. I couldn't find an example of that, but based on the documentation I think that this should do:
asyncPut(WB_RES::LOCAL::TIME(),
AsyncRequestOptions::Empty,
(int64_t)0);
In this case, I'm just trying to reset the epoch to zero but onPutResults gives me
HTTP_CODE_BAD_REQUEST
So what is the right way?
Minimum timestamp seems to be 1483228800000000 us which corresponds to 1.1.2017. So you can't set the time to 70's as zero would set it to.
This should be documented in the yaml api but currently is not. We will add that to the list of tasks to make sure it's documented in the next release of device-lib.

How does resource.data.size() work in firestore rules (what is being counted)?

TLDR: What is request.resource.data.size() counting in the firestore rules when writing, say, some booleans and a nested Object to a document? Not sure what the docs mean by "entries in the map" (https://firebase.google.com/docs/reference/rules/rules.firestore.Resource#data, https://firebase.google.com/docs/reference/rules/rules.Map) and my assumptions appear to be wrong when testing in the rules simulator (similar problem with request.resource.data.keys().size()).
Longer version: Running into a problem in Firestore rules where not being able to update data as expected (despite similar tests working in the rules simulator). Have narrowed down the problem to point where can see that it is a rule checking for request.resource.data.size() equaling a certain number.
An example of the data being passed to the firestore update function looks like
Object {
"parentObj": Object {
"nestedObj": Object {
"key1": Timestamp {
"nanoseconds": 998000000,
"seconds": 1536498767,
},
},
},
"otherKey": true,
}
where the timestamp is generated via firebase.firestore.Timestamp.now().
This appears to work fine in the rules simulator, but not for the actual data when doing
let obj = {}
obj.otherKey = true
// since want to set object key name dynamically as nestedObj value,
// see https://stackoverflow.com/a/47296152/8236733
obj.parentObj = {} // needed for adding nested dynamic keys
obj.parentObj[nestedObj] = {
key1: fb.firestore.Timestamp.now()
}
firebase.firestore.collection('mycollection')
.doc('mydoc')
.update(obj)
Among some other rules, I use the rule request.resource.data.size() == 2 and this appears to be the rules that causes a permission denied error (since commenting out this rules get things working again). Would think that since the object is being passed with 2 (top-level) keys, then request.resource.data.size()=2, but this is apparently not the case (nor is it the number of keys total in the passed object) (similar problem with request.resource.data.keys().size()). So there's a long example to a short question. Would be very helpful if someone could clarify for me what is going wrong here.
From my last communications with firebase support around a month ago - there were issues with request.resource.data.size() and timestamp based security rules for queries.
I was also told that request.resource.data.size() is the size of the document AFTER a successful write. So if you're writing 2 additional keys to a document with 4 keys, that value you should be checking against is 6, not 2.
Having said all that - I am still having problems with request.resource.data.size() and any alternatives such as request.resource.size() which seems to be used in this documentation
https://firebase.google.com/docs/firestore/solutions/role-based-access
I also have some places in my security rules where it seems to work. I personally don't know why that is though.
Been struggling with that for a few hours and I see now that the doc on Firebase is clear: "the request.resource variable contains the future state of the document". So with ALL the fields, not only the ones being sent.
https://firebase.google.com/docs/firestore/security/rules-conditions#data_validation.
But there is actually another way to ONLY count the number of fields being sent with request.writeFields.size(). The property writeFields is a table with all the incoming fields.
Beware: writeFields is deprecated and may stop working anytime, but I have not found any replacement.
EDIT: writeFields apparently does not work in the simulator anymore...