function replace(str, object) {
return String(str).replace((/\\?\{([^{}]+)\}/g), function(match, name) {
return (object[name] != null) ? object[name] : match;
});
};
var string= 'Hello {world}!';
console.log(replace(str, {world:'Pesho'}));
another way (seems to have some issues, sometimes)
var str = function(str, o) {
return str.replace(/{([^{]+)}/g, function(tag, key){
return !o[key] ? tag : o[key];
});
};
console.log(str('Hello my name is {name} & my age is {age}.', {
name:'Michelangelo'
}));
show variables like 'query_cache_size';
If the result is 0 like in the following example, then the query cache is not enabled:
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| query_cache_size | 0 |
+------------------+-------+
If the result is greater than zero then it is enabled. The following example shows a query cache that is 50MB in size:
+------------------+----------+
| Variable_name | Value |
+------------------+----------+
| query_cache_size | 52428800 |
+------------------+----------+
MySQL has a query cache which caches the results of SELECT queries, if enabled. This means that frequently used database queries will run much faster, because the data resultset will be read from the cache instead of having to run the query again. The MySQL query cache is available from MySQL 4.0.1. Whenever tables in the database are modified the relevant entries in the query cache are flushed so you can be certain that even with the query cache enabled only up to date data is returned.
You can tell if the query cache is enabled, and what parameters are set, by running the following query in MySQL:
SHOW VARIABLES LIKE '%query_cache%';
An example result for the above query is below, which shows that the query cache engine is available, but the query cache size is set to zero and therefore nothing will be cached, and the query cache engine will not actually be used.
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 0 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+---------+
It is possible to set the query_cache_size variable without actually restarting the MySQL server, by running the following SQL query. In this example, we are enabling a 50MB query cache.
SET GLOBAL query_cache_size = 50*1024*1024;
Running SHOW VARIABLES LIKE '%query_cache%' will now return the following:
+------------------------------+----------+
| Variable_name | Value |
+------------------------------+----------+
| have_query_cache | YES |
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 52428800 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+----------+
This means a query cache will now be running, using 50MB of memory. However, the next time the MySQL server is restarted the setting will be lost and query cache will no longer be used. Next, we will look at how to make the change permanent.
SET global general_log = 1;
SET global log_output = 'table';
You can then select from my mysql.general_log
table to retrieve recent queries.
I can then do something similar to tail -f
on the mysql.log
, but with more refinements...
select * from mysql.general_log
where event_time > (now() - INTERVAL 8 SECOND) and thread_id not in(9 , 628)
and argument <> "SELECT 1" and argument <> ""
and argument <> "SET NAMES 'UTF8'" and argument <> "SHOW STATUS"
and command_type = "Query" and argument <> "SET PROFILING=1"
This makes it easy to see my queries that I can try and cut back. I use 8 seconds interval to only fetch queries executed within the last 8 seconds.
For SELECTs:
show global status like "Com_select";
UPDATEs:
show global status like "Com_update";
INSERTs:
show global status like "Com_insert";
DELETEs:
show global status like "Com_delete";
ALl values are "cumulativ" since MySQL last restart.
So to get your SELECTs in one hour:
21:00
[21:00:00] [DEV\(none)] mysql> show global status like "Com_select";
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| Com_select | 671664 |
+---------------+--------+
1 row in set (0.00 sec)
22:00
[22:00:00] [DEV\(none)] mysql> show global status like "Com_select";
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| Com_select | 672363 |
+---------------+--------+
1 row in set (0.00 sec)
The number of SELECT in the past hour : 672363 - 671664 = 699
Once all of the APIs return promises, it should be relatively rare that you need to construct one by hand. In the meantime, we need a way to polyfill existing APIs. For example:
function readFile(filename, enc){
return new Promise(function (fulfill, reject){
fs.readFile(filename, enc, function (err, res){
if (err) reject(err);
else fulfill(res);
});
});
}
We use new Promise
to construct the promise. We give the constructor a factory function which does the actual work. This function is called immediately with two arguments. The first argument fulfills the promise and the second argument rejects the promise. Once the operation has completed, we call the appropriate function.