我通过phpredis使用redis作为缓存商店.它工作得很好,我希望提供一些自动防故障方法,以确保缓存功能始终正常(例如,使用基于文件的缓存),即使redis服务器出现故障,最初我想出了以下代码
<?php
$redis=new Redis();
try {
$redis->connect('127.0.0.1', 6379);
} catch (Exception $e) {
// tried changing to RedisException, didn't work either
// insert codes that'll deal with situations when connection to the redis server is not good
die( "Cannot connect to redis server:".$e->getMessage() );
}
$redis->setex('somekey', 60, 'some value');
Run Code Online (Sandbox Code Playgroud)
但是当redis服务器关闭时,我得到了
PHP Fatal error: Uncaught exception 'RedisException' with message 'Redis server went away' in /var/www/2.php:10
Stack trace:
#0 /var/www/2.php(10): Redis->setex('somekey', 60, 'some value')
#1 {main}
thrown in /var/www/2.php on line 10 …Run Code Online (Sandbox Code Playgroud) 我试图使用以下代码监视与node.js'watchFile()(软)符号链接的文件:
var fs=require('fs')
, file= './somesymlink'
, config= {persist:true, interval:1};
fs.watchFile(file, config, function(curr, prev) {
if((curr.mtime+'')!=(prev.mtime+'')) {
console.log( file+' changed');
}
});
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,./ somesymlink是/ path/to///file的(软)符号链接.当对/ path/to////文件进行更改时,不会触发任何事件.我必须用/ path/to//// file替换符号链接才能使它工作.在我看来,watchFile无法观看符号链接的文件.当然我可以通过使用spawn + tail方法来完成这项工作,但我不想使用该路径,因为它会引入更多开销.
所以我的问题是如何使用watchFile()在node.js中观看符号链接文件.提前谢谢大家.
嗨伙计们,我有一个非常实用的redis用例问题.假设我想使用以下js代码存储Redis的平均请求时间.基本上我试图计算平均请求时间并在每个请求条目时保存到redis([req_path,req_time])
var rc=require('redis').createClient()
,rc2=require('redis').createClient()
,test_data=[
['path/1', 100]
,['path/2', 200]
,['path/1', 50]
,['path/1', 70]
,['path/3', 400]
,['path/2', 150]
];
rc.del('reqtime');
rc.del('reqcnt');
rc.del('avgreqtime');
for(var i=0, l=test_data.length; i<l; i++) {
var item=test_data[i], req_path=item[0], req_time=item[1];
console.log('debug: iteration # %d, item=%j', i, item);
rc.zincrby('reqtime', req_time, req_path );
rc.zincrby('reqcnt', 1, req_path, function(err, c) {
rc2.zscore('reqtime', req_path, function(err, t) {
var avg=t/c;
console.log('req_path='+req_path+',t='+t+',c='+c);
console.log('debug: added member %s to sorted set "avgreqtime" with score %f', req_path, avg);
rc2.zadd('avgreqtime', avg, req_path);
});
});
}
rc.quit();
rc2.quit();
Run Code Online (Sandbox Code Playgroud)
但它没有像avgreqtime键那样按预期工作.从我得到的stdout …