Skip to content

Instantly share code, notes, and snippets.

@sthairno
Created December 27, 2024 22:21
Show Gist options
  • Save sthairno/84d6af45cca6912c22315ca7860f78ef to your computer and use it in GitHub Desktop.
Save sthairno/84d6af45cca6912c22315ca7860f78ef to your computer and use it in GitHub Desktop.
OpenSiv3Dにredis-plus-plusを組み込んでみた実験
#include <Siv3D.hpp> // Siv3D v0.6.15
#include <sw/redis++/redis++.h>
void Main()
{
Window::Resize(400, 300);
// Redisサーバーに接続
sw::redis::ConnectionOptions options;
options.type = sw::redis::ConnectionType::TCP;
options.host = "127.0.0.1";
options.port = 6379;
auto redis = sw::redis::Redis(options);
// countが存在しないときゼロ初期化
redis.setnx("count", "0");
// 実行時間計測用
Stopwatch stw;
std::queue<double> durationHistory;
double durationSum = 0.0;
double durationAvg = 0.0;
// 表示用font
const Font font{ 100 };
while (System::Update())
{
// ボタンクリック時にcountの値をインクリメント
if (Scene::Rect().leftClicked())
{
redis.incr("count");
}
// countの値を取得
stw.restart();
const auto countOpt = redis.get("count"); // -> std::optional<std::string>
double duration = stw.msF();
// getメソッドの実行時間の平均を計算
durationHistory.push(duration);
durationSum += duration;
if (durationHistory.size() > 100)
{
durationSum -= durationHistory.front();
durationHistory.pop();
}
durationAvg = durationSum / durationHistory.size();
// 実行時間を表示
ClearPrint();
Print << U"Time: {:01.2}ms/req"_fmt(durationAvg);
// カーソル位置に円を描画
Circle{ Cursor::PosF(), 20 }
.draw(MouseL.pressed() ? Palette::Yellow : Palette::Orange);
// countの値をでっかく表示
if (countOpt)
{
const auto count = Unicode::FromUTF8(*countOpt);
font(count)
.drawAt(Scene::Center(), Palette::White);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment