1+ <?php
2+
3+ namespace SimpleSoftwareIO \Cache ;
4+
5+ use Illuminate \Cache \TaggableStore ;
6+ use Illuminate \Database \Eloquent \Collection ;
7+ use Illuminate \Support \Facades \Cache ;
8+
9+ class QueryCache
10+ {
11+ /**
12+ * The cache store to use.
13+ *
14+ * @var string
15+ */
16+ protected $ store ;
17+
18+ /**
19+ * The amount of time to store the cache.
20+ *
21+ * @var int
22+ */
23+ protected $ length = 30 ;
24+
25+ /**
26+ * QueryCache constructor.
27+ *
28+ * @param string $store
29+ * @param int $length
30+ */
31+ public function __construct ($ store , $ length )
32+ {
33+ $ this ->store = $ store ;
34+ $ this ->length = $ length ;
35+ }
36+
37+ /**
38+ * Returns the status of the cache.
39+ *
40+ * @return bool
41+ */
42+ public function enabled ()
43+ {
44+ return $ this ->length === 0 ? false : true ;
45+ }
46+
47+ /**
48+ * Sets the length of the cache.
49+ *
50+ * @param int $minutes
51+ */
52+ public function length ($ minutes )
53+ {
54+ $ this ->length = $ minutes ;
55+ }
56+
57+ /**
58+ * Enables caching on the model.
59+ *
60+ * @param int $minutes
61+ */
62+ public function enable ($ minutes = 30 )
63+ {
64+ $ this ->length ($ minutes );
65+ }
66+
67+ /**
68+ * Disables the cache on the model.
69+ */
70+ public function disable ()
71+ {
72+ $ this ->length = 0 ;
73+ }
74+
75+ /**
76+ * Gets the model results.
77+ *
78+ * @param QueryCacheBuilder $builder
79+ * @param array $columns
80+ * @return Collection
81+ */
82+ public function get (QueryCacheBuilder $ builder , $ columns = ['* ' ])
83+ {
84+ if (! $ this ->enabled ()) return $ this ->performQuery ($ builder , $ columns );
85+
86+ $ key = $ this ->generateKey ($ builder , $ columns );
87+
88+ $ cache = $ this ->getCache ($ builder );
89+
90+ return $ cache ->remember ($ key , $ this ->length , function () use ($ builder , $ columns ) {
91+ return $ this ->performQuery ($ builder , $ columns );
92+ });
93+ }
94+
95+ /**
96+ * Gets a Cache instance
97+ *
98+ * @return Cache
99+ */
100+ protected function getCache (QueryCacheBuilder $ builder )
101+ {
102+ return $ this ->isTaggable () ? Cache::store ($ this ->store )->tags ($ this ->getTag ($ builder )) : Cache::store ($ this ->store );
103+ }
104+
105+ /**
106+ * Determines if the cache store support tagging.
107+ *
108+ * @return bool
109+ */
110+ protected function isTaggable ()
111+ {
112+ return Cache::getStore () instanceof TaggableStore;
113+ }
114+
115+ /**
116+ * Performs the query on the model.
117+ *
118+ * @param QueryCacheBuilder $builder
119+ * @param array $columns
120+ * @return mixed
121+ */
122+ protected function performQuery (QueryCacheBuilder $ builder , $ columns = ['* ' ])
123+ {
124+ return call_user_func ([$ builder , 'parent::get ' ], $ columns );
125+ }
126+
127+ /**
128+ * Generates the cache key.
129+ *
130+ * @param QueryCacheBuilder $builder
131+ * @param array $columns
132+ * @return string
133+ */
134+ protected function generateKey (QueryCacheBuilder $ builder , array $ columns )
135+ {
136+ $ sql = $ builder ->select ($ columns )->toSql ();
137+ $ whereClause = serialize ($ builder ->getBindings ());
138+
139+ return sha1 ($ sql .$ whereClause );
140+ }
141+
142+ /**
143+ * Returns the tag to tag a cache.
144+ *
145+ * @param QueryCacheBuilder $builder
146+ * @return string
147+ */
148+ protected function getTag (QueryCacheBuilder $ builder )
149+ {
150+ return $ builder ->from ;
151+ }
152+
153+ /**
154+ * Flushes the cache for a model.
155+ *
156+ * @param $tag
157+ * @return mixed
158+ */
159+ public function flush ($ tag )
160+ {
161+ if ($ this ->isTaggable ()) return Cache::tags ($ tag )->flush ();
162+
163+ return Cache::flush ();
164+ }
165+ }
0 commit comments