Skip to content

Commit 6ec2220

Browse files
committed
Merge pull request PHPIDS#53 from staabm/patch-3
adjusted Storage to use less memory and trigger less cache writes
2 parents e9665ff + 006ad82 commit 6ec2220

File tree

1 file changed

+106
-69
lines changed

1 file changed

+106
-69
lines changed

lib/IDS/Filter/Storage.php

Lines changed: 106 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -225,47 +225,51 @@ public function getFilterFromXML()
225225
/*
226226
* Now the storage will be filled with IDS_Filter objects
227227
*/
228-
$data = array();
229228
$nocache = $filters instanceof \SimpleXMLElement;
230-
$filters = $nocache ? $filters->filter : $filters;
231-
232-
foreach ($filters as $filter) {
233-
$id = $nocache ? (string) $filter->id :
234-
$filter['id'];
235-
$rule = $nocache ? (string) $filter->rule :
236-
$filter['rule'];
237-
$impact = $nocache ? (string) $filter->impact :
238-
$filter['impact'];
239-
$tags = $nocache ? array_values((array) $filter->tags) :
240-
$filter['tags'];
241-
$description = $nocache ? (string) $filter->description :
242-
$filter['description'];
243-
244-
$this->addFilter(
245-
new \IDS\Filter(
246-
$id,
247-
$rule,
248-
$description,
249-
(array) $tags[0],
250-
(int) $impact
251-
)
252-
);
253-
254-
$data[] = array(
255-
'id' => $id,
256-
'rule' => $rule,
257-
'impact' => $impact,
258-
'tags' => $tags,
259-
'description' => $description
260-
);
261-
}
262-
263-
/*
264-
* If caching is enabled, the fetched data will be cached
265-
*/
266-
if ($this->cacheSettings) {
267-
268-
$this->cache->setCache($data);
229+
230+
if ($nocache)
231+
{
232+
// build filters and cache them for re-use on next run
233+
$data = array();
234+
$filters = $filters->filter;
235+
236+
foreach ($filters as $filter) {
237+
$id = (string) $filter->id;
238+
$rule = (string) $filter->rule;
239+
$impact = (string) $filter->impact;
240+
$tags = array_values((array) $filter->tags);
241+
$description = (string) $filter->description;
242+
243+
$this->addFilter(
244+
new \IDS\Filter(
245+
$id,
246+
$rule,
247+
$description,
248+
(array) $tags[0],
249+
(int) $impact
250+
)
251+
);
252+
253+
$data[] = array(
254+
'id' => $id,
255+
'rule' => $rule,
256+
'impact' => $impact,
257+
'tags' => $tags,
258+
'description' => $description
259+
);
260+
}
261+
262+
/*
263+
* If caching is enabled, the fetched data will be cached
264+
*/
265+
if ($this->cacheSettings) {
266+
$this->cache->setCache($data);
267+
}
268+
269+
} else {
270+
271+
// build filters from cached content
272+
$this->addFiltersFromArray($filters);
269273
}
270274

271275
return $this;
@@ -313,52 +317,85 @@ public function getFilterFromJson()
313317
/*
314318
* Now the storage will be filled with IDS_Filter objects
315319
*/
316-
$data = array();
317320
$nocache = !is_array($filters);
318-
$filters = $nocache ? $filters->filters->filter : $filters;
319-
320-
foreach ($filters as $filter) {
321-
322-
$id = $nocache ? (string) $filter->id :
323-
$filter['id'];
324-
$rule = $nocache ? (string) $filter->rule :
325-
$filter['rule'];
326-
$impact = $nocache ? (string) $filter->impact :
327-
$filter['impact'];
328-
$tags = $nocache ? array_values((array) $filter->tags) :
329-
$filter['tags'];
330-
$description = $nocache ? (string) $filter->description :
331-
$filter['description'];
332-
333-
$this->addFilter(
321+
322+
if ($nocache) {
323+
324+
// build filters and cache them for re-use on next run
325+
$data = array();
326+
$filters = $filters->filters->filter;
327+
328+
foreach ($filters as $filter) {
329+
330+
$id = (string) $filter->id;
331+
$rule = (string) $filter->rule;
332+
$impact = (string) $filter->impact;
333+
$tags = array_values((array) $filter->tags);
334+
$description = (string) $filter->description;
335+
336+
$this->addFilter(
334337
new \IDS\Filter(
335338
$id,
336339
$rule,
337340
$description,
338341
(array) $tags[0],
339342
(int) $impact
340-
)
341-
);
342-
343-
$data[] = array(
343+
)
344+
);
345+
346+
$data[] = array(
344347
'id' => $id,
345348
'rule' => $rule,
346349
'impact' => $impact,
347350
'tags' => $tags,
348351
'description' => $description
349-
);
350-
}
351-
352-
/*
353-
* If caching is enabled, the fetched data will be cached
354-
*/
355-
if ($this->cacheSettings) {
356-
$this->cache->setCache($data);
352+
);
353+
}
354+
355+
/*
356+
* If caching is enabled, the fetched data will be cached
357+
*/
358+
if ($this->cacheSettings) {
359+
$this->cache->setCache($data);
360+
}
361+
362+
} else {
363+
364+
// build filters from cached content
365+
$this->addFiltersFromArray($filters);
357366
}
358367

359368
return $this;
360369
}
361370

362371
throw new \RuntimeException('json extension is not loaded.');
363372
}
373+
374+
/**
375+
* This functions adds an array of filters to the IDS_Storage object.
376+
* Each entry within the array is expected to be an simple array containing all parts of the filter.
377+
*
378+
* @param array $filters
379+
*/
380+
private function addFiltersFromArray(array $filters)
381+
{
382+
foreach ($filters as $filter) {
383+
384+
$id = $filter['id'];
385+
$rule = $filter['rule'];
386+
$impact = $filter['impact'];
387+
$tags = $filter['tags'];
388+
$description = $filter['description'];
389+
390+
$this->addFilter(
391+
new \IDS\Filter(
392+
$id,
393+
$rule,
394+
$description,
395+
(array) $tags[0],
396+
(int) $impact
397+
)
398+
);
399+
}
400+
}
364401
}

0 commit comments

Comments
 (0)