1 /*
2  Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights
3  reserved.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License, version 2.0,
7  as published by the Free Software Foundation.
8 
9  This program is also distributed with certain software (including
10  but not limited to OpenSSL) that is licensed under separate terms,
11  as designated in a particular file or component or in included license
12  documentation.  The authors of MySQL hereby grant you an additional
13  permission to link the program and your derivative works with the
14  separately licensed software that they have included with MySQL.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License, version 2.0, for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24  02110-1301  USA
25  */
26 
27 #include <my_config.h>
28 #include <time.h>
29 
30 #include "memcached/types.h"
31 #include "memcached/server_api.h"
32 
33 #include <NdbApi.hpp>
34 
35 #include "ExpireTime.h"
36 #include "Operation.h"
37 
38 
ExpireTime(workitem * i)39 ExpireTime::ExpireTime(workitem *i) :
40   item(i),
41   ndb_expire_time(0),
42   is_expired(false)
43 {
44   SERVER_CORE_API * SERVER = item->pipeline->engine->server.core;
45   current_time = SERVER->get_current_time();
46 
47   /*** NOTE regarding items read from NDB and then cached locally.
48    *** Expire times in the local cache are currently HARD-CODED, HERE.
49    *** Data retrieved from memory expires after 5 seconds.
50    *** Data retrieved from disk expires after 300 seconds.
51    ***/
52   if(item->plan->hasDataOnDisk())
53     local_cache_expire_time = current_time + 300;
54   else
55     local_cache_expire_time = current_time + 5;
56 }
57 
58 
59 /*  stored_item_has_expired()
60     If the timestamp is a MySQL 5.6 fractional-second timestamp,
61     only the integer part is considered.
62 */
stored_item_has_expired(Operation & op)63 bool ExpireTime::stored_item_has_expired(Operation &op) {
64   SERVER_CORE_API * SERVER = item->pipeline->engine->server.core;
65   time_t stored_exptime;
66 
67   if(item->prefix_info.has_expire_col && ! op.isNull(COL_STORE_EXPIRES)) {
68     stored_exptime = op.getIntValue(COL_STORE_EXPIRES);
69     ndb_expire_time = SERVER->realtime(stored_exptime);
70 
71     if(ndb_expire_time > 0) {
72       /* Has the item already expired? */
73       if(ndb_expire_time < current_time)
74         is_expired = true;
75 
76       /* Don't keep it in memory for longer than it is valid in the db */
77       if(ndb_expire_time < local_cache_expire_time)
78         local_cache_expire_time = ndb_expire_time;
79     }
80   }
81   return is_expired;
82 }
83 
84