-2

Now I have some mappings, A->(A1, A2, A3, A4), B->(B1, B2, B3)

I wanna set the expire time of A1, A2, A3... as two days, so I can only get the values added within two days. And if all the values of A expired, the key A expired and should be deleted.

What solution or database can achieve this feature? Thanks very much.

####################################################-

Well, I should describe this problem in more detail.

A lot of key-value pairs have been stored in Couchbase. And the key-value pairs are like this: {id1_20170306-00:01:00 => value1} {id1_20170306-10:01:00 => value2} {id2_20170306-00:01:00 => value3}. The expiration time for all key-value pairs is two days.

When the user sends a request for id1, all the key-value pairs whose keys start with id1 and not expired should be returned.

I have a solution that is not particularly good.

Convert the key to the start of the day. For 20170306-00:01:00 and 20170306-10:01:00, it is 20170306-00:00:00. Create a mapping like this id1_20170306-00:00:00 => (id1_20170306-00:01:00,id1_20170306-10:01:00).

Set the expiration time of id1_20170306-00:00:00 to two days after the last modification.

When the user sends a request for id1, keys like id1_20170305-00:00:00 and id1_20170306-00:00:00 are generated to access the real keys stored in Couchbase.

Actually, the survive time of id1_20170306-00:01:00 is over two days.

Brooklyn Knightley
  • 335
  • 1
  • 5
  • 16
  • Which DBMS are you talking about? –  Mar 06 '17 at 11:00
  • @a_horse_with_no_name Any database will do, as long as I can achieve this goal – Brooklyn Knightley Mar 06 '17 at 11:02
  • I think you problem's solution is related to this http://stackoverflow.com/questions/35407858/how-would-you-expire-or-update-a-mysql-record-with-precision-to-the-expiry-tim#35408917 – Siddharth Tyagi Mar 06 '17 at 11:03
  • @BrooklynKnightley *"I wanna set the expire time of A1, A2, A3... as two days, so I can only get the values added within two days. And if all the values of A expired, the key A expired and should be deleted." you will not find any database having that feature "out of the box", you will have to program it. – Timothy Truckle Mar 06 '17 at 11:03

2 Answers2

1

Sounds like you want two different things:

  1. A query that only returns recent items
  2. A clean-up job that runs regularly and deletes expired items

Both of these tasks are trivial and can be done with any relational and many NoSQL databases.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

SUMMARY

Use the application layer to solve the problem rather than database layer. Based on your input from the question, I believe there are two set of entities -S1(A, B) and S2(Ai, Bi). To do a cleanup you can implement a job to periodically cleanup S2 based on a timeout (cached in entity store structure itself). And cleanup S1 accordingly. But the effective change in real time is subject to performance of the overall system.

DETAILED

A database will not provide a ready to consume solution for your specific problem, as they are designed to store data and query rather than being a universal business solution.

I believe, the result that you seek can be acquired with:

Database/store Level

You will need to extend an existing open source database (as you need the source code for that) and accommodate your functions accordingly. But as per your specific solution, it will always be a tradeoff between overall querying performance - depending on the extent you want it to reflect in real time.

Application Level

This is the best solution. As you can solve in multiple and optimal ways. The ultimate goal being the eradication of records that are no longer valid. I will suggest a daemon process to query and cleanup such records. You can use a database to provide efficient querying and synchronisation of cleanup across multiple clients (as long as you are not reading dirty). The optimisation of cleanup daemon depends on a number of factors that are not described within the problem.

Siddharth Tyagi
  • 395
  • 2
  • 8