2018年10月18日 星期四

Redis on Windows Visual Studio C++



https://blog.csdn.net/LG1259156776/article/details/54645745


https://github.com/MicrosoftArchive/redis
Download and extract redis-3.0.zip
Open redis-3.0\msvs\RedisServer.sln with Visual Studio Community 2017 v15.8.3
Compile hiredis and Win32_interop projects to generate hiredis.lib and Win32_Interop.lib in redis-3.0\msvs\x64\Debug
Visual Studio > New > Visual C++ > Empty Project > Project1

1. Copy hiredis.lib and Win32_Interop.lib to Project1 folder
2. Copy *.h from redis-3.0\deps\hiredis\ to Project1\deps\hiredis

async.h
dict.h
fmacros.h
hiredis.h
net.h
sds.h
win32_hiredis.h
zmalloc.h

3. Overwrite fmacros.h from redis-3.0\src\fmacros.h to Project1\deps\hiredis\fmacros.h
4. Copy *.h from redis-3.0\src\Win32_Interop\ to Project1\src\Win32_Interop\
5. Copy win32fixes.c from redis-3.0\src\Win32_Interop\win32fixes.c to Project1\win32fixes.c
6. Select x64, Project1 > Properties > C/C++ > General > Additional Include Directories > .\deps\hiredis\;.\src\;

7. create a.cpp and compile


#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <ctime>

#include "hiredis.h"
#define NO_QFORKIMPL // must add this line
#include "Win32_Interop\win32fixes.h"
#pragma comment(lib,"hiredis.lib")  
#pragma comment(lib,"Win32_Interop.lib")  

int main() {

 using namespace std;

 unsigned int j;
 redisContext *c;
 redisReply *reply;

 struct timeval timeout = { 1, 500000 }; // 1.5 seconds  
 c = redisConnectWithTimeout((char*)"127.0.0.1", 6379, timeout);
 if (c->err) {
  printf("Connection error: %s\n", c->errstr);
  exit(1);
 }

 reply = (redisReply *)redisCommand(c, "AUTH foobared");
 printf("AUTH: %s\n", reply->str);
 freeReplyObject(reply);

 clock_t begin = clock();
 for (int i = 0;i<1000; ++i) {
  reply = (redisReply *)redisCommand(c, "publish redisChat %d", i);
  //printf("publish: %s\n", reply->str);
  freeReplyObject(reply);
 }
 clock_t end = clock();
 double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
 printf("elapsed_secs = %f\n", elapsed_secs); // 0.1s for 1000 commands

 /* PING server */
 reply = (redisReply *)redisCommand(c, "PING");
 printf("PING: %s\n", reply->str);
 freeReplyObject(reply);

 /* Set a key */
 reply = (redisReply *)redisCommand(c, "SET %s %s", "foo", "hello world");
 printf("SET: %s\n", reply->str);
 freeReplyObject(reply);

 /* Set a key using binary safe API */
 /*reply = (redisReply *)redisCommand(c, "SET %b %b", "bar", 3, "hello", 5);
 printf("SET (binary API): %s\n", reply->str);
 freeReplyObject(reply);*/

 /* Try a GET and two INCR */
 reply = (redisReply *)redisCommand(c, "GET foo");
 printf("GET foo: %s\n", reply->str);
 freeReplyObject(reply);

 reply = (redisReply *)redisCommand(c, "INCR counter");
 printf("INCR counter: %lld\n", reply->integer);
 freeReplyObject(reply);
 /* again ... */
 reply = (redisReply *)redisCommand(c, "INCR counter");
 printf("INCR counter: %lld\n", reply->integer);
 freeReplyObject(reply);

 /* Create a list of numbers, from 0 to 9 */
 reply = (redisReply *)redisCommand(c, "DEL mylist");
 freeReplyObject(reply);
 for (j = 0; j < 10; j++) {
  char buf[64];

  sprintf_s(buf, 64, "%d", j);
  reply = (redisReply *)redisCommand(c, "LPUSH mylist element-%s", buf);
  freeReplyObject(reply);
 }

 /* Let's check what we have inside the list */
 reply = (redisReply *)redisCommand(c, "LRANGE mylist 0 -1");
 if (reply->type == REDIS_REPLY_ARRAY) {
  for (j = 0; j < reply->elements; j++) {
   printf("%u) %s\n", j, reply->element[j]->str);
   getchar();
  }
 }
 freeReplyObject(reply);

 return 0;

}

Design Data Structure in Redis


Redis Setup
https://redislabs.com/blog/redis-on-windows-10/
Install Ubuntu on Windows by "Windows Feature" GUI or by command line:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

> sudo apt-get update
> sudo apt-get upgrade
> sudo apt-get install redis-server
> redis-cli -v

> $ sudo vi /etc/redis/redis.conf
requirepass foobared

> sudo service redis-server restart

127.0.0.1:6379> get foo
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth foobared
OK
127.0.0.1:6379> get foo
"bar"


Data Structure

class Summary : public Item {
double orders;
double buyVolume;
double buyPrice;
double sellVolume;
double sellPrice;
double profit;
};
class Quote : public Item {

double bid;
double ask;
};

std::unordered_map<std::string, Summary> items;
std::unordered_map<std::string, Quote> items;

Redis Hashes
https://www.tutorialspoint.com/redis/redis_data_types.htm
Redis hashes are key-value pairs.
redis 127.0.0.1:6379> HMSET user:1 username tutorialspoint password 
tutorialspoint points 200 
OK 
redis 127.0.0.1:6379> HGETALL user:1  
1) "username" 
2) "tutorialspoint" 
3) "password" 
4) "tutorialspoint" 
5) "points" 
6) "200"

The above represents

class User {
std::string username;
std::string password;
int points;
};
std::unordered_map<std::string, User> users;
users["1"] = User{"name", "password", 200};


Python get set

import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0, password='foobared')
r.set('foo', 'bar')
print(r.get('foo'))


Python subscribe to Redis
import redis

def main():
    try:
        r = redis.StrictRedis(host='localhost', port=6379, db=0, password='foobared')
        p = r.pubsub()
        p.subscribe('redisChat')
        done = False
        while not done:
            message = p.get_message()
            if message:
                data = message['data']
                if isinstance(data, bytes):
                    print(data)
    except Exception as e:
        print(e)

if __name__=='__main__':
    main()



The End

2018年10月11日 星期四

Event schedule in MySQL 8.0



https://dev.mysql.com/doc/refman/5.7/en/create-event.html

1. Create a sample database

use usersdb;
create table users (id int, name varchar(32), time datetime);
insert into users values (1, "a", NOW());
select * from users;






2. Create event

delimiter |
create event if not exists FiveSeconds on schedule EVERY 5 second
COMMENT 'Saves total number of sessions then clears the table each day'
do
begin
insert into users values (2, "a", NOW());
insert into users values (3, "a", NOW());
set @id = 4;
insert into users values (@id, "a", NOW());
set @id = 5;
insert into users values (@id, "a", NOW());
END |
delimiter ;





































The End


2018年10月1日 星期一

Multiple RDP Sessions on Windows 10 Hyper-V

Hyper V
=======

Hyper V
Version 2 (New)
win10 pro
4096MB
No Check Point
Install from DVD

RDPWrap
=======

https://github.com/stascorp/rdpwrap/releases
RDPWrap-v1.6.2.zip
admin cmd.exe > install.bat

net user
========

net user /add user01 Passw0rd
net localgroup administrators user01 /add
net localgroup "Remote Desktop Users" user01 /add

net user /add user02 Passw0rd
net localgroup administrators user02 /add
net localgroup "Remote Desktop Users" user02 /add

net user /add user03 Passw0rd
net localgroup administrators user03 /add
net localgroup "Remote Desktop Users" user03 /add

net user /add user04 Passw0rd
net localgroup administrators user04 /add
net localgroup "Remote Desktop Users" user04 /add

net user /add user05 Passw0rd
net localgroup administrators user05 /add
net localgroup "Remote Desktop Users" user05 /add

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...