redis methods
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2024-07-10 15:08:47 +00:00
parent 129034fbea
commit 080b7c93aa
2 changed files with 105 additions and 0 deletions

93
functions.php Normal file
View File

@@ -0,0 +1,93 @@
<?php
$REDIS_HOST='redis';
function ping_redis() {
global $REDIS_HOST;
$redis = new Redis();
$redis->connect($REDIS_HOST);
if ($redis->ping()) return true;
else return false;
}
function check_redis($group="webin") {
global $REDIS_HOST;
$redis = new Redis();
$redis->connect($REDIS_HOST);
if ($redis->ping()) {
$members = $redis->sMembers($group); // redis-cli -h safebox-redis smembers generated
//print_r($members);
foreach ($members as $member) {
$value = $redis->get($member);
$json_data = base64_decode($value);
$data = json_decode($json_data);
if ($data === null) {
echo "JSON read error...";
// TODO json error
}
else {
}
}
}
}
function redis_get($key) {
global $REDIS_HOST;
$redis = new Redis();
$redis->connect($REDIS_HOST);
if ($redis->ping()) {
//$arList = $redis->keys("*"); // ? redis-cli -h safebox-redis keys "*"
//echo "Stored keys in redis:";
//print_r($arList);
if ($redis->exists($key)) {
$value = $redis->get($key);
//redis-cli -h safebox-redis get $key
return base64_decode($value);
} else {
echo "Key does not exist: $key";
// TODO
}
}
}
function redis_set($key, $value) {
global $REDIS_HOST;
$redis = new Redis();
$redis->connect($REDIS_HOST);
// $redis->auth('password');
if ($redis->ping()) {
if (!$redis->exists($key)) {
//redis-cli -h redis set $key "$value"
//redis-cli -h redis sadd webout $key
//redis-cli -h redis smembers webout
$redis->set($key, base64_encode($value));
$redis->sAdd('webout', $key);
} else {
echo "Key already exist: $key";
}
}
}
function redis_remove($key) {
$redis = new Redis();
$redis->connect($REDIS_HOST);
// $redis->auth('password');
if ($redis->ping()) {
//redis-cli -h redis srem webin $key
//redis-cli -h redis del $key
$redis->srem("webin", $key);
$redis->del($key);
}
}
?>

12
scan.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
switch ($_GET["op"]) {
"redis":
echo ping_redis();
"docker":
echo true;
break;
}
?>