CacheMogul » Automate disk-caching

 

CacheMogul automates the process of storing files to disk. Many operating systems do not handle storing many thousands of files in a single directory, and because of this, you need to create sub-directories, and sub-directories of sub-directories. It can get pretty confusing and overwhelming--especially if you have millions of files to store.

how does it work?

With CacheMogul, you can store thousands, or billions of files with little more effort than it takes to write a single file to disk.

It works by transparently managing the necessary overhead to make storing your files possible.

why should you use CacheMogul?

If you manage a community that allows users to upload files you know that managing those uploads can sometimes be painful. With CacheMogul, you just need:
$CM->write($user_id.'user-file.png', $data). Then to read that data:
$CM->read($user_id.'user-file.png');.

 

how to write a file to disk with CacheMogul

Typically, CacheMogul will replace the line of code that opens a file for writing with PHP's fopen function.

require('CacheMogul.php');
$CM = new CacheMogul;
$f = $CM->open('a-filename.txt');
fwrite($f, 'hello world');
fclose($f);

You can also use it to replace file_put_contents:

$CM->write('a-filename.txt', $data);

Or use it to move a file from another location:

$CM->move('/path/to/some-file.txt');

Remember! PHP needs read/write access to your cache directory.

get the CacheMogul PHP class

CacheMogul is available free of charge. If you find it useful and it allows you to make a few extra bucks, please consider heading over to my donate page and giving me one. » Download » View with highlighting

some examples:

All of the examples require the following two lines at the top:


<?php
require("CacheMogul.php");
$CM = new CacheMogul;
?>

Handling a file upload from a user:


<?php
$user_id 
$FROM_DATABASE["user_id"];
$tmp_name $_FILES["an-upload"]["tmp_name"];

// prepending user_id to the filename so that there are no collisions between users
$filename $user_id.$_FILES["an-upload"]["name"]; 

// CMs generate_path will create a diskcache path that move_uploaded_file can use
move_uploaded_file($tmp_name$CM->generate_path($filename)); 
?>

Writing data to a file:


<?php
$CM
->write("some-filename.txt""this is my data!");
?>

Saving output from a URL:


<?php
$url 
"http://www.google.com/search?q=a+google+search";
$h fopen($url"r");
$CM->write_stream($url$hfalse);

// write_stream accepts a third parameter (T/F) that will close the handle (defaults to true)
fclose($h);
?>

Saving file history:


<?php
$CM
->cvs true;

$CM->write("hello-world.txt""some text");
$CM->write("hello-world.txt""some other text");
$CM->write("hello-world.txt""still, some more text");
?>

On disk:
cache/98/f2/98f22c2dca8bf5ef2fe42552a9f95ec7 «- Newest version of file "still, some more text"
cache/98/f2/98f22c2dca8bf5ef2fe42552a9f95ec7.0 «- Oldest version of file "some text"
cache/98/f2/98f22c2dca8bf5ef2fe42552a9f95ec7.1 «- "some other text"

Deleting a file:


<?php
$CM
->delete("some-filename.txt");
?>
Written by Cory Mawhorter