ec_cache_insert — Insert a key/value pair into a cache
ec_cache_elt * **ec_cache_insert** ( | cache, | |
| | key, | |
| | keylen, | |
| | value
); | |
Insert a key/value pair into a cache.
The address of the cache. The following typedef applies to the
typedef struct ec_cache_head ec_cache_t;.
Specifies a binary blob that is used to identify the entry in the cache. It will be copied into the “ec_cache_elt” structure. This may also be a simple
NULterminated string. The cache will make a copy of this, so the memory that you supply may be a transient buffer on the stack.
The length of the key, in bytes. The caching infrastructure makes no assumption about the NUL terminated-ness of the key, which is why you must supply the correct length of the memory using this parameter.
The address of the value to insert into the cache. This is an arbitrary pointer to the data that you want to cache. If an existing value with the same key is present in the cache, its reference count will be decremented and the new value you are inserting will take its place.
Returns the address of the “ec_cache_elt” structure associated with the value.
You are responsible for calling ec_cache_delref on this structure when you are done using
value. This is to ensure that the cache doesn’t cause it to be deleted and to ensure that allocated memory is released.
#include "config.h" #include "ec_cache.h" ... ec_cache_elt *elt; elt = ec_cache_insert(cache, "key", strlen("key"), value); /* you can use value here if you want */ ec_cache_delref(cache, elt); /* after this point, you MUST NOT touch value if your destructor * frees its memory */ ...
It is legal to call this function in any thread.