xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / inc / cache.inc.php (a242a7b1542eb0264fabd62840fbbf1c232fed3c) (11KiB) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
<?php
//
// memcache alike daemon
//
require_once($INC . "/util.inc.php");
require_once($INC . "/prof.inc.php");

// Client side can disable the cache for various reasons (unit testing etc.)
if (!isset($rg_cache_enable))
	$rg_cache_enable = TRUE;
if (!isset($rg_cache_core_enable))
	$rg_cache_core_enable = TRUE;

// timeout in miliseconds
$rg_cache_timeout = 500;

$rg_cache_count = 0;
$rg_cache_tries = 3;

if (!isset($rg_cache_socket))
	$rg_cache_socket = "/var/lib/rocketgit/sockets/cache.sock";

$rg_cache = array();

$rg_cache_error = "";

if (!isset($rg_cache_debug))
	$rg_cache_debug = FALSE;

function rg_cache_set_error($str)
{
	global $rg_cache_error;
	$rg_cache_error = $str;
	rg_log($str);
}

function rg_cache_error()
{
	global $rg_cache_error;
	return $rg_cache_error;
}

/*
 * Clears all core cache
 */
function rg_cache_core_destroy()
{
	global $rg_cache;
	global $rg_cache_core_enable;

	if (!$rg_cache_core_enable)
		return;

	$rg_cache = array();
}

/*
 * Dump all tables
 */
function rg_cache_core_dump()
{
	global $rg_cache;
	global $rg_cache_core_enable;

	if (!$rg_cache_core_enable)
		return '';

	return "rg_cache:\n" . print_r($rg_cache, TRUE);
}

/*
 * Sets a variable
 * TODO: Prevent cache to grow and grow.
 */
function rg_cache_core_set($ns_var, $value)
{
	global $rg_cache;
	global $rg_cache_core_enable;

	if (!$rg_cache_core_enable)
		return;

	//rg_log_ml("cache_core_set: $ns_var = " . print_r($value, TRUE));

	$tree = &$rg_cache;
	$t = explode("::", $ns_var);
	$var = array_pop($t);
	foreach ($t as $token) {
		if (!isset($tree[$token]))
			$tree[$token] = array();

		$tree = &$tree[$token];
	}

	$tree[$var] = $value;
}

/*
 * Merges some items into a variable
 */
function rg_cache_core_merge($ns_var, $list)
{
	global $rg_cache;
	global $rg_cache_core_enable;

	if (!$rg_cache_core_enable)
		return;

	//rg_log_ml("cache_core_merge: $ns_var = " . print_r($list, TRUE));

	$tree = &$rg_cache;
	$t = explode("::", $ns_var);
	foreach ($t as $token) {
		if (!isset($tree[$token]))
			$tree[$token] = array();

		$tree = &$tree[$token];
	}

	foreach ($list as $k => $v)
		$tree[$k] = $v;

	return TRUE;
}

/*
 * Increments a variable
 */
function rg_cache_core_inc($ns_var)
{
	global $rg_cache;
	global $rg_cache_core_enable;

	if (!$rg_cache_core_enable)
		return;

	$tree = &$rg_cache;
	$t = explode("::", $ns_var);
	$var = array_pop($t);
	foreach ($t as $token) {
		if (!isset($tree[$token]))
			$tree[$token] = array();

		$tree = &$tree[$token];
	}

	if (!isset($tree[$var]))
		$ret = 1;
	else
		$ret = $tree[$var] + 1;

	$tree[$var] = $ret;

	return $ret;
}

/*
 * Retrieve a variable from cache
 */
function rg_cache_core_get($ns_var)
{
	global $rg_cache;
	global $rg_cache_core_enable;

	if (!$rg_cache_core_enable)
		return FALSE;

	//rg_log("cache_core_get: $ns_var");

	$tree = &$rg_cache;
	$t = explode("::", $ns_var);
	$var = array_pop($t);
	foreach ($t as $token) {
		if (!isset($tree[$token])) {
			//rg_log("CHECK: cache_core_get: token '$token' not found, return false");
			return FALSE;
		}

		$tree = &$tree[$token];
	}

	if (isset($tree[$var])) {
		//rg_log_ml("CHECK: cache_core_get: found key in cache: $ns_var = " . print_r($tree[$var], TRUE));
		return $tree[$var];
	}

	//rg_log_ml("CHECK: cache_core_get: [$ns_var] not found in rg_cache. rg_cache: " . print_r($rg_cache, TRUE));
	return FALSE;
}

/*
 * Unset a variable in cache
 * Returns FALSE if not found, else TRUE.
 */
function rg_cache_core_unset($ns_var)
{
	global $rg_cache_debug;
	global $rg_cache;
	global $rg_cache_core_enable;

	if (!$rg_cache_core_enable)
		return;

	$tree = &$rg_cache;
	$t = explode("::", $ns_var);
	$var = array_pop($t);
	foreach ($t as $token) {
		if (!isset($tree[$token])) {
			//rg_log("CHECK: cache_core_unset: token [$token] not found");
			return FALSE;
		}

		$tree = &$tree[$token];
	}

	if (isset($tree[$var])) {
		if ($rg_cache_debug)
			rg_log('cache_core_unset(' . $ns_var . ')');
		unset($tree[$var]);
		return TRUE;
	}

	//rg_log("CHECK: cache_core_unset: key [$var] not found");
	return FALSE;
}

/*
 * Push a variable in a queue
 */
function rg_cache_core_apush($ns_var, $value)
{
	global $rg_cache;
	global $rg_cache_core_enable;

	if (!$rg_cache_core_enable)
		return;

	$tree = &$rg_cache;
	$t = explode("::", $ns_var);
	$var = array_pop($t);
	foreach ($t as $token) {
		if (!isset($tree[$token]))
			$tree[$token] = array();

		$tree = &$tree[$token];
	}

	if (!isset($tree[$var]))
		$tree[$var] = array();

	array_push($tree[$var], $value);
}

/*
 * Pop a variable from the end of a queue
 */
function rg_cache_core_apop($ns_var)
{
	global $rg_cache;
	global $rg_cache_core_enable;

	if (!$rg_cache_core_enable)
		return FALSE;

	$tree = &$rg_cache;
	$t = explode("::", $ns_var);
	$var = array_pop($t);
	foreach ($t as $token) {
		if (!isset($tree[$token]))
			return FALSE;

		$tree = &$tree[$token];
	}

	if (!isset($tree[$var]))
		return FALSE;

	if (empty($tree[$var]))
		return FALSE;

	return array_pop($tree[$var]);
}

/*
 * Pops and returns a variable from the begining of a queue
 */
function rg_cache_core_ashift($ns_var)
{
	global $rg_cache;
	global $rg_cache_core_enable;

	if (!$rg_cache_core_enable)
		return FALSE;

	$tree = &$rg_cache;
	$t = explode("::", $ns_var);
	$var = array_pop($t);
	foreach ($t as $token) {
		if (!isset($tree[$token]))
			return FALSE;

		$tree = &$tree[$token];
	}

	if (!isset($tree[$var]))
		return FALSE;

	if (empty($tree[$var]))
		return FALSE;

	return array_shift($tree[$var]);
}

/*
 * Dumps a queue
 */
function rg_cache_core_adump($ns_var)
{
	global $rg_cache;
	global $rg_cache_core_enable;

	if (!$rg_cache_core_enable)
		return FALSE;

	$tree = &$rg_cache;
	$t = explode("::", $ns_var);
	$var = array_pop($t);
	foreach ($t as $token) {
		if (!isset($tree[$token]))
			return FALSE;

		$tree = &$tree[$token];
	}

	if (!isset($tree[$var]))
		return FALSE;

	return rg_array2string($tree[$var]);
}


/********************************* Client side functions */

/*
 * Prepares a string to be send to the wire (key part)
 */
function rg_cache_prepare_key($s)
{
	return addcslashes($s, "\n\r\\");
}

/*
 * Prepares a string to be send to the wire (value part)
 */
function rg_cache_prepare($s)
{
	$x = serialize($s);
	return addcslashes($x, "\n\r\\");
}

/*
 * Helps to send and to decode a command
 */
function rg_cache_send($cmd, $para, $flags)
{
	global $rg_cache_enable;
	global $rg_cache_socket;
	global $rg_cache_timeout;
	global $rg_cache_tries;
	global $rg_cache_count;
	global $rg_cache_debug;

	if ($rg_cache_enable === FALSE)
		return FALSE;

	$rg_cache_count++;

	$f = '';
	if ($flags & RG_SOCKET_NO_WAIT)
		$f .= 'W';

	$xcmd = $cmd . ' F=' . $f . ' I=' . $rg_cache_count . ' ' . $para;
	if ($rg_cache_debug)
		rg_log('Sending [' . $xcmd . ']...');
	$ret = rg_socket($rg_cache_socket, $xcmd . "\n",
		$rg_cache_timeout, $rg_cache_tries, $flags);
	if ($rg_cache_debug)
		rg_log('Received [' . $ret . ']');
	if ($ret === FALSE)
		return FALSE;

	if ($flags & RG_SOCKET_NO_WAIT)
		return TRUE;

	$a = explode("\n", $ret);
	foreach ($a as $line) {
		//if ($rg_cache_debug)
		//	rg_log('Parsing line [' . $line . ']');

		$t = explode(' ', $line, 3);
		if (!isset($t[1]))
			return FALSE;

		$status = $t[0];
		$id = intval($t[1]);
		if ($id < $rg_cache_count) {
			if ($rg_cache_debug)
				rg_log('DEBUG: id: ' . $id . ' < ' . $rg_cache_count);
			continue;
		}
		//if ($rg_cache_debug)
		//	rg_log('DEBUG: id: ' . $id . ' == ' . $rg_cache_count);

		if (strcmp($status, 'OK') != 0) {
			//rg_log('DEBUG: not an OK answer: ' . $status);
			return FALSE;
		}

		if (!isset($t[2]))
			return TRUE;

		return trim(stripcslashes($t[2]));
		break;
	}
}

/*
 * Returns a variable from the cache daemon
 */
function rg_cache_get($ns_var)
{
	global $rg_cache_debug;

	rg_prof_start('cache_get');

	$ret = FALSE;
	while (1) {
		$k = rg_cache_prepare_key($ns_var);
		$ret = rg_cache_core_get($k);
		if ($ret !== FALSE)
			break;

		$flags = 0;
		$r = rg_cache_send('GET', $k, $flags);
		if ($r === FALSE)
			break;

		$ret = @unserialize($r);
		if ($ret === FALSE) {
			rg_internal_error("Cannot userialize [$r]!");
			break;
		}

		rg_cache_core_set($ns_var, $ret);
		break;
	}

	if ($rg_cache_debug)
		rg_log_ml('cache_get[' . $ns_var . '] returns: '
			. ($ret === FALSE ? 'FALSE' : print_r($ret, TRUE)));

	rg_prof_end('cache_get');
	return $ret;
}

/*
 * Sets a variable in the cache daemon
 */
function rg_cache_set($ns_var, $value, $flags)
{
	global $rg_cache_debug;

	rg_prof_start('cache_set');

	while (1) {
		$k = rg_cache_prepare_key($ns_var);
		rg_cache_core_set($k, $value);

		$para = $k . '=' . rg_cache_prepare($value);
		$ret = rg_cache_send('SET', $para, $flags);
		if ($ret === FALSE)
			break;

		if ($rg_cache_debug)
			rg_log_ml('cache_set[' . $ns_var . '] = ' . print_r($value, TRUE));

		$ret = TRUE;
		break;
	}

	rg_prof_end('cache_set');
	return $ret;
}

/*
 * Increments a variable in the cache daemon
 */
function rg_cache_inc($ns_var)
{
	rg_prof_start('cache_inc');

	rg_cache_core_inc($vs_var);

	while (1) {
		$k = rg_cache_prepare_key($ns_var);
		$ret = rg_cache_send('INC', $k, $flags);
		if ($ret === FALSE)
			break;
		if ($ret === TRUE)
			break;

		$ret = intval($ret);
		break;
	}

	rg_prof_end('cache_inc');
	return $ret;
}

/*
 * Unsets a variable in the cache daemon
 */
function rg_cache_unset($ns_var, $flags)
{
	global $rg_cache_debug;

	rg_prof_start('cache_unset');

	$k = rg_cache_prepare_key($ns_var);
	rg_cache_core_unset($k);
	$ret = rg_cache_send('UNSET', $k, $flags);

	if ($rg_cache_debug)
		rg_log('cache_unset(' . $k . ')');

	rg_prof_end('cache_unset');
	return $ret;
}

/*
 * Merge some k=v pairs into an existing cache
 */
function rg_cache_merge($ns_var, $list, $flags)
{
	global $rg_cache_debug;

	rg_prof_start('cache_merge');

	$k = rg_cache_prepare_key($ns_var);
	rg_cache_core_merge($k, $list);

	$para = $k . '=' . rg_cache_prepare($list);
	$ret = rg_cache_send('MERGE', $para, $flags);

	if ($rg_cache_debug)
		rg_log_ml('cache_merge[' . $ns_var . '] = ' . print_r($list, TRUE));

	rg_prof_end('cache_merge');
	return $ret;
}

/*
 * Push an array in a queue
 */
function rg_cache_apush($ns_var, $value, $flags)
{
	rg_prof_start('cache_apush');

	$k = rg_cache_prepare_key($ns_var);
	rg_cache_core_apush($k, $value);

	$para = $k . '=' . rg_cache_prepare($value);
	$ret = rg_cache_send('APUSH', $para, $flags);

	rg_prof_end('cache_apush');
	return $ret;
}

/*
 * Restarts the cache daemon
 */
function rg_cache_restart()
{
	$flags = 0;
	$para = '';
	$ret = rg_cache_send('SHUTDOWN', $para, $flags);
	if ($ret === FALSE)
		return FALSE;

	rg_log('Cache restarted.');
	return TRUE;
}

/*
 * Just to debug stuff
 */
function rg_cache_sleep()
{
	$flags = RG_SOCKET_NO_WAIT;
	$para = '';
	$ret = rg_cache_send('SLEEP', $para, $flags);
	if ($ret === FALSE)
		return FALSE;

	return TRUE;
}

?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master