@@ -46,6 +46,24 @@ class LdapAuth extends BaseObject
46
46
*/
47
47
public $ filterBySidhistory = false ;
48
48
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
+
49
67
private $ _ldapBaseDn ;
50
68
private $ _l ;
51
69
private $ _username ;
@@ -59,6 +77,15 @@ public function init()
59
77
throw new Exception ("LDAP-extension missing :( " );
60
78
}
61
79
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
+
62
89
// Sort the domains one time for this run!
63
90
$ autoDetectDomainKey = $ this ->autoDetect ();
64
91
@@ -360,6 +387,34 @@ public function searchUser(?string $searchFor, ?array $attributes = [], ?string
360
387
throw new InvalidArgumentException ("Search term is empty but the filter has a placeholder set! Set a term or set a new filter. " );
361
388
}
362
389
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
+
363
418
// Default set
364
419
$ domains = $ this ->domains ;
365
420
@@ -471,6 +526,27 @@ public function searchUser(?string $searchFor, ?array $attributes = [], ?string
471
526
Yii::debug ("Result: " , __METHOD__ );
472
527
Yii::debug ($ return , __METHOD__ );
473
528
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
+
474
550
return empty ($ return ) ? [] : $ return ;
475
551
476
552
0 commit comments