File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,9 @@ trait TAjaxSelect
2525 /** @var callable */
2626 private $ onchange ;
2727
28+ /** @var \Nette\Caching\Cache */
29+ private $ storage ;
30+
2831 public function setCallback (callable $ callback ): self
2932 {
3033 $ this ->callback = $ callback ;
@@ -37,6 +40,12 @@ public function setOnchange(callable $onchange) : self
3740 return $ this ;
3841 }
3942
43+ public function setCacheStorage (\Nette \Caching \Cache $ storage ) : self
44+ {
45+ $ this ->storage = $ storage ;
46+ return $ this ;
47+ }
48+
4049 public function getControl (): \Nette \Utils \Html
4150 {
4251 $ this ->initiateItems ();
@@ -94,12 +103,25 @@ private function getData(string $query = '', $default = null): array
94103 throw new \Nette \InvalidStateException ('Callback for " ' . $ this ->getHtmlId () . '" is not set. ' );
95104 }
96105
106+ if ($ this ->storage instanceof \Nette \Caching \Cache) {
107+ $ cacheKey = $ this ->getHtmlId () . '_ ' . $ query . '_ ' . $ default ;
108+ $ result = $ this ->storage ->load ($ cacheKey );
109+
110+ if (\is_array ($ result ) && !empty ($ result )) {
111+ return $ result ;
112+ }
113+ }
114+
97115 $ data = \call_user_func ($ this ->callback , $ query , $ default );
98116
99117 if (!\is_array ($ data )) {
100118 throw new \Nette \InvalidStateException ('Callback for " ' . $ this ->getHtmlId () . '" must return array. ' );
101119 }
102120
121+ if ($ this ->storage instanceof \Nette \Caching \Cache) {
122+ $ this ->storage ->save ($ cacheKey , $ data );
123+ }
124+
103125 return $ data ;
104126 }
105127
You can’t perform that action at this time.
0 commit comments