Set expiry time for a session field - ruby-on-rails-3

Is there a way in Rails 3 to expire a field in the session? When I searched for this I only found posts about how to expire the whole session, which I do not want to do.
In my situation I have the following:
session["END_DATE"] = #end_date
I want to tell Rails to expire the field session["END_DATE"] after a certain amount of time, which is different and a lot less than the global expiry time for the whole session.
I can always use a cookie instead of a session for that field, but it would be good to have it in the session if it is possible.

you can do something like this:
session.delete(:end_date) to delete something(end_date in your case) from your session.
Hope it helps!

Related

Is there a way to increase ttl in redis?

I know there are several ways to set a specific ttl for a key, but is there a way to add some extra time for a key which has a counting down ttl?
There's no built-in way to extend TTL. You need to get the current TTL, and then add some more TTL to it.
Wrap these two steps into a Lua script:
-- extend 300 seconds
eval 'local ttl = redis.call("TTL", "key") + 300; redis.call("EXPIRE", "key", ttl)' 0
Good question
there is no such command
I think it is a bad idea to have a command like that, you have to be careful when you use it.
Probably end up adding more time to the ttl than we expect. If you set it like 5 mins, the actual expire time will be close to 5 mins even if setting it multiple times in that request. But if you add multiple 5 mins to it, then we can`t be sure of the actual expire time

Redis: How to update a key but do not change it's expiration time?

Using Redis as cache service to cache some non-important data, and there is a case that need to update the value without reset or override the expire time, is there any good way to resolve this problem?
I've searched and found follows 2 solutions
Using setrange command, since the value is a little more complex, so it is not good in this situation.
Get the ttl time then set it as the expiration time when update the value. it's seems a little more redundant.
Any good idea to resolve this problem?
You don't need to do any of those two things. You just need to use the KEEPTTL flag when you set your value.
Like this:
> set my_key this_is_my_value EX 60
This will set a value for a key with 60 seconds expiration.
Then, when you change the value and don't want to change the expiration of your key, just do:
> set my_key this_is_my_new_value KEEPTTL
More information on REDIS docs.
Another idea to so resolve this could be using INCRBY.
For this you have to do some steps.
Get the existing value. For example, 10.
Determine the update value.For example, 17 .
INCRBY the value of their difference 17-10. That means, 7
This won't change the ttl

Get Yii DB Cache id of saved record in cache table

I have a query like this:
$record=books::model()->cache(10000)->find('id=:id',array(":id"=>$id));
Yii uses CDbCache to save the result in the cache table
id expire value
-- ------ ------
My question is:
How to get the id of the cache record -this id is generated by yii - in
yii cache table?
There's no public method to get the cache ID, as it incorporates a lot of variables, you could if you really wanted to but it's really dark down in the depths of Yii.
Answer based on comments:
You could always set a last cached time on your record, which you can then use to calculate the time remaining. But this won't persist over page spans, because your caching you result and it won't get read again. So your options are:
Do the caching manually, and store the time against the object to be used for calculation
Or store the time for each ID pulled in the session, and use that to calculate it.
To Answer this question. By the way the Yii code is written, you can't get the ID/Key for the Cache which the YII generates internally.
If you look at the sourcecode
https://github.com/yiisoft/yii/blob/1.1.16/framework/db/CDbCommand.php
Method queryInternal
You'll realize that the cacheKey generation is the complex thing. Moreover this cacheKey will further be Hashed by MD5
Refer: http://www.yiiframework.com/doc/api/1.1/CCache#generateUniqueKey-detail
If you are using 'cache' as part of QueryBuilder, then we are giving the power to Yii to manage this cache by providing the Expire Duration and Dependency Query.
If you want to be in command/control of the Expire Duration and want to have the ID, then only way is to manually set the CacheID as suggested by the user
Paystey as belowt:
$dbResult = Yii::app()->cache->get('YOUR-KNOWN-KEY');
if($dbResult === false){
$dbResult = YOUR-QUERY;
Yii::app()->cache->set('YOUR-KNOWN-KEY, $dbResult);
}

How to know if key set to be expired or not in Redis?

As far as Redis do not allow to reSet expire date to key (because of nans with replication) I'd like to know is there any method to check if key set to be expired or not?
Thank you
Use the TTL command. If an expiration is set, it returns the number of seconds until the key expires; otherwise it returns -1.
I don't think checking for expiration date make much sense in Redis, though. I'd like to first suggest that you model it so you don't need to check for expiration date.
If you really need it, though, you can just use another key to store the expiration date for later retrieval via normal GET/SET.
Note that you can also check for EXPIRES manually in your client code, which might be a better solution.

Can I set Session timeout for a specified Session variable differently than other Session Variables

I'd like to set the timeout on a specific Session Variable in a .Net web application, but leave other Session variables alone. Is this possible?
Example:
I have 5 Session Variables
Session(var1)
Session(var2)
Session(var3)
Session(var4)
Session(var5)
I want to set it so that Session(var1) through Session(var4) have a timeout of 8 hours (480 minutes), but Session(var5) has a timeout of 20 minutes. Can it be done?
You may implement this feature by creating timestamp field in this variable (or associated timestamp field). The timestamp will mark the time after which the variable becomes invalid. Of course you will have to handle the timestamp manually in your code (update it per hit, and remove it from session when it becomes invalid).
You would need to use an alternative to Session for storage. The Caching framework of asp.net is probably the closest alternative. Refer to Adding Items to the Cache.
You could provide your own implementation of HttpSessionStateBase your version would be aware of the different things it can store and change when they are flushed from the session.