Remove keys from cache - c++

I'm using memcached and c++. I want to remove all keys from server using c++ api. It would be better to remove them without list of the keys.
There is function in documentation: memcached_dump and memcached_delete. First one returns the list of keys, and the second one - removes them.
But here is the quote from the docs of first function:
memcached_dump() is used to get a list of keys found in memcached(1)
servers. Because memcached(1) does not guarentee to dump all keys you
can not assume you have fetched all keys from the server.
The first question: any ways to fetch ALL keys and the second is: how to use these functions at all. There aren't any examples in documentation.
Thanks.

Sounds like you want memcached_flush ?

An elegant way to remove the memcached keys would be the use of basic delete command.
But as we don't know which keys to delete, you ought to keep a log of the data being set in the memcached. You could dump these log along with their time-stamp in any data-store. By this procedure you would be able to delete keys with certain rules thereby providing to better control in the delete operation.

Logging keys is a useful way of managing cache data when you need to be able to delete a bunch of keys. In addition, using a prefix can provide a way of managing the cached data as a whole.
function save($key,$data,$group){
cache_log_key($group,$key);
cache_save($application_prefix.$key,$data);
}
function deleteGroup($group){
$loggedKeys = cache_get_log($group);
foreach($loggedKeys as $key){
cache_delete($application_prefix.$key);
}
cache_delete_log($group);
}

Related

Why use pagination tokens?

I am implementing pagination on a webservice. My first thought was to use query params page and size, like Spring Data.
However, we are basing some of our design on the google webservice apis. I notice that they use pagination tokens, with each page result containing a nextPageToken. What are the advantages to using this approach? Changing data? What kind of info would be encoded in such a token?
When paginating with an offset, inserts and deletes into the data between your requests will cause rows to be skipped or included twice. If you are able to keep track in the token of what you returned previously (via a key or whatever), you can guarantee you don't return the same result twice, even when there are inserts/deletes between requests.
I'm a little uncertain of how you would encode a token, but for a single tables at least it seems that you could use the an encoded version of the primary key as a limit. "I just returned everything before key=200. Next time I'll only return things after 200." I guess this assumes a new item inserted between requests 1 and 2 will be given a key greater than existing keys.
https://use-the-index-luke.com/no-offset
One reason opaque strings are used for pagination tokens is so that you can change how pagination is implemented without breaking your clients. A query param like a page(I assume you mean page number) is transparent to your client and allows them to make assumptions about it.

Play Framework cache Remove elements matching regex

I was wondering if there is a way to delete elements from the Play cache using a regex.
I'm using play 2.2.x and I'm storing elements in the cache following this pattern:
collectionName.identifier
Is there a way to expire the caché using a regular expression to match the key, like:
collectionName.[a-zA-Z0-9]+
The reason I want to do that is because sometimes I will update elements in db matching some fields, and I can't really know which elements were updated.
If there is a way in ReactiveMongo to get the updated object identifiers, that would help me as well.
Thanks for any help.
Play's cache uses Ehcache underneath. You'll need to work with Ehcache directly.
Access the underlying Ehcache object using app.plugin[EhCachePlugin].cache (see the plugin source).
Then call the Ehcache object's getKeys method to get the list of cache entry keys.
Then match the keys yourself and remove any entries that match your regex.
By the way, it would be better if you update or remove items from the cache when you update the database.

purge sitecore analytics geoip records inside request pipeline

Our analytics database has alot of empty data stored in cachedValue. This data seems to get returned forever. I want to basically check that the required data is present in the cache, and if not, then do a new lookup.
I also want to allow testers to trigger this fresh lookup using a query string variable.
I've used reflector and noticed some Invalidate functions, but this appears to just clear cookies or remove the data from the in memory cache. It looks like I want to set the cachedGeoIpHandle.GeoResolveState = Failed, not sure how to do this yet.
I also see exactly where I could make this happen in the GeoIpManager, but I can override, extend that class.
Thanks.

How can I find the InternetRegistry User Key or Parent Registry Key

I have a BHO which on the first run is gathering activation information and storing this in the registry.
(I think) due to IE's permission's I am only able to store this in the registry branch
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\InternetRegistry\REGISTRY\USER\S-0-0-00-000000000-000000000-000000000-0000\Software\MyBHO\MyKey
Where S-0-0-00-000000000-000000000-000000000-0000 is a unique key for each user.
Which is fine using RegCreateKey() with "Software\MyBHO\MyKey". It's all created and running lovely. It determines where in space to store the Key with no problems.
The Problem:
When I carry out an uninstall I want to remove this key and as this is run outside of IE I have no way to determine where that key is / what the user string is.
Options I have in mind:
Option 1 (Ideal)
Find out this user string first to then build a new path for the key I wish to remove and remove it. How?
Option 2
At the point of activation store the path to the key in another registry value that can be accessed. Then read, and delete both (Which seems a bit backwards and probably wont work due to the access restrictions of the BHO on the registry (Thus it being written there in the first place))
Do you know if there is any way to find this User key or even how to find the parent dir.
Edit Upon continued research I've found that the thing I'm referring to as "user key" is the current Users "SID". Maybe this will yield me better results.
Call GetUserName to get the user name, and LookupAccountName to get his SID.
ConvertSidToStringSid is a useful utility function to format a SID as a S-1-5-32-00000000-00000000-00000000-00000000-0000 string
If you really want to write per-user data to the registry, use IEGetWriteableHKCU().
In general there is no good way to remove per-user data at uninstall. For example, what if you install as user A and the uninstall as user B? Are you going to go find all of them and delete them? Just leave the turds behind.
Alternatively you could consider using a different data store. Do you really need the registry? Can you store this data in a file? What about Web Storage?

Django Caching - How to generate custom key names?

Right now, I am retrieving information from an API, and I would like to cache the information I get back, so I do not have to constantly hit their server and use up my max API call requests. Right now, a user can search up a particular keyword, like "grapes", I would like to cache the retrieved string by calling "cache.set(search_result, info_retrieved, 600)" where "search_result" is the user's search result, in this case, "grapes". I want the key to be the user's search result, which is "grapes". I cannot do this since the cache requires the key to be a string. How can I get around this? I cannot use a database because the information updates too often.
I could use a database, but I would be writing information to it, then deleting it after a few minutes, which seems impractical. So, I just want to cache it temporarily.
As Shawn Chin mentioned, you should already have a string "version" of your search query, which would work just fine as a cache key.
One limitation with memcached (not sure about other backends) is that certain characters (notably, spaces) are not allowed in keys. The easiest way to get around this is to hash your string key into a hex digest and use that as a key:
from hashlib import sha1
key = sha1('grapes').hexdigest() # '35c4cdb50a9a6b4475da4a66d955ef2a9e1acc39'
If you might have different results for different users (or based on whatever criteria), you can tag/salt/flavor the key with a string representation of that information:
from hashlib import sha1
key = sha1('%s:%s:%s' % (user.id, session.sessionid, 'grapes')).hexdigest()
You could also use django-newcache:
Newcache is an improved memcached cache backend for Django. It provides four major advantages over Django's built-in cache backend:
It supports pylibmc.
It allows for a function to be run on each key before it's sent to memcached.
It supports setting cache keys with infinite timeouts.
It mitigates the thundering herd problem.
It also has some pretty nice defaults. By default, the function that's run on each key is one that hashes, versions, and flavors the key. More on that later.