skip to Main Content

My project is create a programs like Memcached. The program is store list of key , values and and the expiry time of the cached item.Its mean after n seconds a data will be remove. I think I can use struct to store key and values, but i can not remove a data after n seconds, Can you give me some solution? Thanks for all

2

Answers


  1. What about using timer?

    You can use a time_t struct and clock() function from time.h.

    Store the start time in a time_t struct by using clock() and check the
    elapsed time by comparing the difference between stored time and
    current time.

    Here is explained.

    Login or Signup to reply.
  2. Just do the delete lazily.

    You don’t need to delete expired data immediately. In order to maintain the semantics of the data store, you only need to do two things:

    1. Not return expired data to a query. (But see below.)

    2. Not allow the datastore to fill up with expired data.

    In other words, it is sufficient to delete expired data when you happen to encounter it, either because it showed up as the response to a query or because it occupies a slot which you need to store an update.

    To simplify detection of expired data, you should store the actual expiry time in the structure, not the time to live. Then it’s easy to see whether a key/value pair has expired: you just compare the expiry time to the current time.

    If you use a chained hash, you can edit the hash chain (by removing expired entries) during a search of that chain. If you use open addressing, you can replaced expired entries with a tombstone (or you can use expiry as a tombstone). In both cases, if you find the key you are looking for but the entry is expired, you can terminate the search, either returning "key not present" if it’s a query or by overwriting the data (and expiry time) with the new data if it’s an update.


    Note:

    1. The data store cannot really guarantee that expired data will never be returned, since it does not control the network latency for responses. It is quite possible that the data it returns had not expired at the moment that it was despatched from the server, but has expired by the time it arrives at the client. So the data store can only offer "best effort", and it is up to the client to decide whether or not to use the data returned (and it is important that the server return the expiry time along with the data as a response to a query).

      Since the client must check the expiry time anyway, it would not be a definitive technical violation of the contract if the data store only checked expiry dates when it was updating an entry. But since the cost of not sending definitely expired data is so small that it’s hardly worth worrying about, it seems reasonable to include the check in queries as well as updates.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search