Skip to content

Commit

Permalink
fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
qmegas committed Jul 18, 2023
1 parent 227c76f commit 433d143
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ composer require qmegas/memcache-search

Requirements
------------
* PHP >= 7.0
* PHP >= 7.1
* Memcache server should work on unix-like system and be >= 1.4.24

Usage Examples
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "qmegas/memcache-search",
"type": "library",
"description": "PHP library to search memcache keys",
"version": "0.1.2",
"version": "0.1.3",
"keywords": [
"memcache",
"memcached",
Expand All @@ -16,7 +16,7 @@
}
],
"require": {
"php": ">=7.0"
"php": ">=7.1"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/BusyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Qmegas\Exception;

class BusyException extends \Exception
{
}
14 changes: 14 additions & 0 deletions src/MemcacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class MemcacheItem
{
private $key = '';
private $expires = 0;
private $lastAccessTime = 0;
private $dataSize = 0;
private $server = '';
private $rawData = '';

Expand All @@ -15,6 +17,8 @@ public function __construct(string $data)
$info = $this->parse($data);
$this->key = urldecode($info['key']);
$this->expires = (int)$info['exp'];
$this->lastAccessTime = (int)$info['la'];
$this->dataSize = (int)$info['size'];
}

public function setServer(string $server)
Expand All @@ -31,6 +35,16 @@ public function getExpiration(): int
{
return $this->expires;
}

public function getLastAccessTime(): int
{
return $this->lastAccessTime;
}

public function getDataSize(): int
{
return $this->dataSize;
}

public function getServer(): string
{
Expand Down
39 changes: 37 additions & 2 deletions src/MemcacheSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,27 @@

class MemcacheSearch
{
public const ON_BUSY_IGNORE = 0;
public const ON_BUSY_EXCEPTION = 1;
public const ON_BUSY_TIMEOUT = 2;

private $servers = [];
private $busyStrategy = self::ON_BUSY_EXCEPTION;
private $busyTimeout = 5;

public function addServer(string $domain, int $port = 11211)
{
$this->servers[] = [$domain, $port];
}

public function onBusyStrategy(int $strategy, int $timeout = 5)
{
$this->busyStrategy = $strategy;

if ($strategy === self::ON_BUSY_TIMEOUT) {
$this->busyTimeout = $timeout;
}
}

/**
* @param string|callable|Finder\FinderInterface $finder
Expand All @@ -21,8 +36,9 @@ public function search($finder)
if (is_string($finder)) {
$finder = new Finder\Inline($finder);
}

foreach ($this->servers as list($domain, $port)) {
$serverBusy = false;
$fp = fsockopen($domain, $port);
if (!$fp) {
throw new Exception\ConnectionException("Can not connect to {$domain}:{$port}");
Expand All @@ -34,13 +50,32 @@ public function search($finder)
while (true) {
$part .= fgets($fp, 1024);
$lines = $this->extractLines($part);

foreach ($lines as $line) {
$line = trim($line);

if ($line === 'END' || $line === 'ERROR' || $line === '') {
break 2;
} elseif (substr($line, 0, 5) === 'BUSY ') {
$excMessage = "Crawler busy for server {$domain}:{$port}";
if ($this->busyStrategy === self::ON_BUSY_IGNORE) {
break 2;
} elseif ($this->busyStrategy === self::ON_BUSY_EXCEPTION) {
fclose($fp);
throw new Exception\BusyException($excMessage);
} elseif ($this->busyStrategy === self::ON_BUSY_TIMEOUT) {
if ($serverBusy) {
fclose($fp);
throw new Exception\BusyException("{$excMessage} after waiting {$this->busyTimeout} seconds");
}

$serverBusy = true;
sleep($this->busyTimeout);
continue;
}
}

$serverBusy = false;

$key = new MemcacheItem($line);
$match = is_callable($finder) ? $finder($key) : $finder->match($key);
Expand Down

0 comments on commit 433d143

Please sign in to comment.