Skip to content

Commit f3ea2ff

Browse files
author
robin.kluth
committed
Optional caching
1 parent d0760ae commit f3ea2ff

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This extensions adds a simple LDAP-Auth mechanism for your yii2 application
1313
* Read self defined LDAP attributes
1414
* Domain autodetection based on IPFilter.
1515
* Filter out results by checking every results `sidHistory`
16+
* Optional query caching
1617

1718
## Installation
1819

@@ -32,6 +33,9 @@ Either you use it as standalone or add this as component:
3233
'ldap' => [
3334
'class' => 'commifreak\yii2\LdapAuth',
3435
'filterBySidhistory' => false, // Filter by checking sidHistory?
36+
'enableCache' => false,
37+
'forceApcuCache' => false,
38+
'apcuCacheTtl' => 3600,
3539
'domains' => [
3640
['name' => 'Domain1', 'hostname' => 'domain1.tld', 'autodetectIps' => ['172.31.0.0/16', '192.168.178.0/24', '127.0.0.1'], 'baseDn' => 'DC=Domain1,DC=tld', 'publicSearchUser' => 'example@domain', 'publicSearchUserPassword' => 'secret'],
3741
['name' => 'Domain2', 'hostname' => '192.168.178.14', 'autodetectIps' => ['192.168.178.55'], 'baseDn' => 'DC=Domain2,DC=tld', 'publicSearchUser' => 'example@domain', 'publicSearchUserPassword' => 'secret'],

src/LdapAuth.php

+76
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ class LdapAuth extends BaseObject
4646
*/
4747
public $filterBySidhistory = false;
4848

49+
/**
50+
* Enable optional object caching. Uses Yii::$app->cache component. Or enable APCu with `forceApcuCache`
51+
* @var bool
52+
*/
53+
public $enableCache = false;
54+
55+
/**
56+
* Force the use of APCu caching instead of the Yii2 cache component
57+
* @var bool
58+
*/
59+
public $forceApcuCache = false;
60+
61+
/**
62+
* Time in seconds objects are cached, if `forceApcuCache` is enabled!
63+
* @var int
64+
*/
65+
public $apcuCacheTtl = 3600;
66+
4967
private $_ldapBaseDn;
5068
private $_l;
5169
private $_username;
@@ -59,6 +77,15 @@ public function init()
5977
throw new Exception("LDAP-extension missing :(");
6078
}
6179

80+
// Check for APCu missing if not cli.
81+
if (php_sapi_name() != 'cli' && $this->enableCache && $this->forceApcuCache && !extension_loaded('apcu')) {
82+
throw new Exception("Caching is enabled but APCU is not! :(");
83+
}
84+
85+
if ($this->enableCache && !$this->forceApcuCache && !isset(Yii::$app->cache)) {
86+
throw new Exception("Caching is enabled with Yii cache component but its not configured! :(");
87+
}
88+
6289
// Sort the domains one time for this run!
6390
$autoDetectDomainKey = $this->autoDetect();
6491

@@ -360,6 +387,34 @@ public function searchUser(?string $searchFor, ?array $attributes = [], ?string
360387
throw new InvalidArgumentException("Search term is empty but the filter has a placeholder set! Set a term or set a new filter.");
361388
}
362389

390+
$cacheKey = 'y2ldap_' . md5($searchFor . (implode("", $attributes)) . $searchFilter);
391+
Yii::debug("Cache-Key: " . $cacheKey, __METHOD__);
392+
393+
if ($this->enableCache) {
394+
if (!$this->forceApcuCache) {
395+
$storedValue = Yii::$app->cache->get($cacheKey);
396+
if ($storedValue) {
397+
Yii::debug("[YII] Returning cached asset", __METHOD__);
398+
return $storedValue;
399+
}
400+
Yii::debug("Was not cached or invalid", __METHOD__);
401+
} else {
402+
if (apcu_exists($cacheKey)) {
403+
Yii::debug("Caching enabled and this search is stored!", __METHOD__);
404+
$apcuValue = apcu_fetch($cacheKey);
405+
if ($apcuValue !== false) {
406+
Yii::debug("[APCU] Returning cached asset!", __METHOD__);
407+
return $apcuValue;
408+
}
409+
Yii::warning("Could not return cached asset!", __METHOD__);
410+
} else {
411+
Yii::debug("No cache entry!", __METHOD__);
412+
}
413+
}
414+
} else {
415+
Yii::debug("Caching disabled", __METHOD__);
416+
}
417+
363418
// Default set
364419
$domains = $this->domains;
365420

@@ -471,6 +526,27 @@ public function searchUser(?string $searchFor, ?array $attributes = [], ?string
471526
Yii::debug("Result:", __METHOD__);
472527
Yii::debug($return, __METHOD__);
473528

529+
if ($this->enableCache) {
530+
Yii::debug("Adding cache entry", __METHOD__);
531+
if (!$this->forceApcuCache) {
532+
if (Yii::$app->cache->set($cacheKey, $result)) {
533+
Yii::debug("[YII] Caching succeeded!", __METHOD__);
534+
} else {
535+
Yii::warning("[YII] Caching failed!", __METHOD__);
536+
}
537+
} else {
538+
$cacheResult = apcu_store($cacheKey, $return, $this->apcuCacheTtl);
539+
if (!$cacheResult) {
540+
Yii::warning("[APCU] Caching was not successful!", __METHOD__);
541+
} else {
542+
Yii::debug("[APCU] Cached!", __METHOD__);
543+
}
544+
}
545+
} else {
546+
Yii::debug("Not caching: Disabled", __METHOD__);
547+
}
548+
549+
474550
return empty($return) ? [] : $return;
475551

476552

0 commit comments

Comments
 (0)