jasonrogena / jasonrogena/php-excel-reader

Support for caching results (patch included)

Open
#12 0 comments 0 reactions 0 assignees View on GitHub
auto-migrated Priority-Medium Type-Defect
Dominant language
PHP
Stars
1
Forks
0
PR merge metrics
No merged PRs in 30d

Description

```
I don't think it's a good idea to process the spreadsheet file over and over
again, especially as it
may not change very frequently.

Here's a little function to cache the results of the spreadsheet read as a PHP
object. Make sure
you create the cache directory and make sure it can be written to.

best,
matt

function Cached_Spreadsheet($filename) {
$cache_dir = './cache'; //set to null to disable cache
$xlsmtime = filemtime($filename);
$cached = false;

// If CACHE ENABLED
if ($cache_dir != '' && file_exists($cache_dir)) {
$cache_file = $cache_dir . '/xlscache_' . md5($filename);
$timediff = @($xlsmtime != filemtime($cache_file));
if ($timediff && file_exists($cache_file)) {
// cached file is fresh enough, return cached array
$result = unserialize(join('', file($cache_file)));
// set 'cached' to 1 only if cached file is correct
if ($result) $cached = true;
} else {
// cached file is too old, create new
$result = new Spreadsheet_Excel_Reader($filename, false);
$serialized = serialize($result);
if ($f = @fopen($cache_file, 'w')) {
fwrite ($f, $serialized, strlen($serialized));
fclose($f);
}
if ($result) $cached = false;
}
} else {
// If CACHE DISABLED >> load and parse the file directly
$result = new Spreadsheet_Excel_Reader($filename, false);
if ($result) $cached = false;
}

if ($cached) echo "\n";

// return result
return $result;
}

$xls = Cached_Spreadsheet("example.xls");
```

Original issue reported on code.google.com by `matt.sep...@gmail.com` on 11 Mar 2009 at 3:53

Attachments:
- [example-cached.php](https://storage.googleapis.com/google-code-attachments/php-excel-reader/issue-12/comment-0/example-cached.php)
- [php-excel-reader-2.zip](https://storage.googleapis.com/google-code-attachments/php-excel-reader/issue-12/comment-0/php-excel-reader-2.zip)

Contributor guide

No contributing guide indexed for this repository

Research direction

Review the attached example-cached.php and php-excel-reader-2.zip, then compare the proposed Cached_Spreadsheet usage with the current reader entry point. Done means repeated reads can reuse a writable cache, changes to the spreadsheet invalidate it, and disabling or unavailable caching still loads the file directly.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend, performance
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.