c++ hiredis

#include <cstdio>
#include <string.h>
#include <hiredis/hiredis.h>

using namespace std;

int main()
{
    redisContext *conn = redisConnect("127.0.0.1", 6379);
    if (conn != NULL && conn->err)
    {
        printf("connection error: %s\n", conn->errstr);
        return 0;
    }

    redisReply *reply;
    for (int i=0;i<10000;i++)
    {
        reply = (redisReply*)redisCommand(conn, "publish mychannel test:{\"username\":\"BlackMamba\",\"password\":123456}"); // publish message 中不能有空格
        freeReplyObject(reply);
    }
    
    redisFree(conn);
    return 0;
}

//g++ -o redis Redis.cpp -lhiredis -levent

c++11 json 处理

c++11 json的库 https://github.com/dropbox/json11

tcp接收json字符串并处理

void *ThreadTcp(void *ptr)
{
int listenfd, connfd;
struct sockaddr_in servaddr;
char buff[4096];
int n;

if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(9060);

if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
printf("bind socket error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}

if( listen(listenfd, 10) == -1){
printf("listen socket error: %s(errno: %d)\n", strerror(errno), errno);
return 0;
}

printf("======waiting for client's request======\n");
while(1){
if( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL)) == -1){
printf("accept socket error: %s(errno: %d)", strerror(errno), errno);
continue;
}
cout << connfd << endl; n = recv(connfd, buff, MAXLINE, 0); buff[n] = '\0'; //printf("recv msg from client: %s\n", buff); string err_comment; auto recv_data = Json::parse(buff, err_comment); Json::array account_list = recv_data.array_items(); for (auto &account_data : account_list) { cout << "account: " << account_data["account"].string_value() << endl; cout << "password: " << account_data["password"].string_value() << endl; } //close(connfd); } close(listenfd); return 0; }

PHP opcache 缓存加密功能

开启opcache扩展后,添加以下的配置
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.validate_timestamps=0
;opcache.revalidate_freq=60
opcache.file_cache=/www/opcache
opcache.file_cache_only=0
opcache.save_comments=0
opcache.enable_file_override=1

将代码使用opcache_compile_file函数编译好后清空源码文件,这样就起到了隐藏源码的功能。

function opcache_compile_files($dir)
{
    $directory = new RecursiveDirectoryIterator($dir);
    $iterator = new RecursiveIteratorIterator($directory);
    foreach ($iterator as $v)
    {
        if (!$v->isDir() && preg_match('%\.php$%', $v->getRealPath())) {
            opcache_invalidate($v->getRealPath(), true);
            opcache_compile_file($v->getRealPath());
            file_put_contents($v->getRealPath(), '');
        }
    }
}
opcache_compile_files('/www/www.baidu.com');
不足的地方是如果重启服务器或者php-fpm 程序就不能运行了。