Archive for September, 2009

Simple general data cache for Codeigniter

Thursday, September 24th, 2009

Here’s a very simple and generic data cache class for Codeigniter:

  • since the end of 2008 renames are atomic on both Windows and Linux
  • I use the filemtime with the ttl, this means no extra expiry file
  • the getter doesn’t return the data, but stores it in a buffer
  • values are stored as serialized php files in the CI cache directory
  • use it as a plugin: $CI-<load-<plugin(‘SerCache’); //I didn’t want it to behave like a library

Hope someone finds it useful. A more advanced cache for CI may be found here and another one here.

class SerCache {
public $filename;
private $tempfile;
private $ttl;
private $default_ext = '.ser';
public $buffer;

/**
* Simple data cache
* @param string name filename of the cache file (probably an md5 hash)
* @param mixed ttl time to live in seconds; -1 circumvents caching, 'forever' is infinite
*/
public function __construct($name, $ttl = 'forever')
{
$CI =& get_instance();
$path = $CI->config->item('cache_path');
$this->cache_dir = ($path == '') ? BASEPATH.'cache/' : $path;

$this->ttl = $ttl;
if ($ttl == -1)
return FALSE;
$this->filename = $this->cache_dir.$name.$this->default_ext;
$this->tempfile = $name.getmypid();
}

/**
* Fetch data into $this->buffer
* @return bool FALSE if expired or not set, TRUE otherwise
*/
public function get()
{
if ($this->ttl == -1)
return FALSE;
if ($this->ttl == 'forever')
{
if (!file_exists($this->filename))
return FALSE;
}
else
{
if ((!file_exists($this->filename)) OR (filemtime($this->filename) + $this->ttl < time()))
return FALSE;
}
$this->buffer = unserialize(file_get_contents($this->filename));
return TRUE;
}

/**
* Save serialized value to file
* @param mixed value value to be serialized
*/
public function put($value) {
if ($this->ttl == -1)
return FALSE;
file_put_contents($this->tempfile, serialize($value));
rename($this->tempfile, $this->filename);
}

/**
* Deletes the cachefile
*/
public function destroy() {
if (file_exists($this->filename))
{
unlink($this->filename);
}
}

/**
* Returns the filemtime of the cache file
*/
public function filemtime() {
if (file_exists($this->filename))
return filemtime($this->filename);
return 0;
}
}

Example:

$cache = new SerCache('something', $cacheTTL);
if ($cache->get()) {
$var = $cache->buffer;
} else {
//create var here...
$cache->put($var);
}

Mouse gestures in Chrome (and elsewhere)

Friday, September 11th, 2009

After checking the gestures extension for Chrome/Chromium I decided to use a small freeware utility instead, StrokeIt, which is free for personal use and can recognize mouse gestures, convert them to shortcuts or messages and send them into applications – and has an extremely small memory footprint. Really nice indeed!

But what’s the problem with the extension? The fact that it uses the document.body to capture the gesture means that it will have a hard time with the mouse crossing document boundaries – iframes for example (which are widely used with banners and widgets/gadgets), so unless you implement the gesture system in the application itself (Opera) or give a greater control to the extension (XUL) this is not a way I want to go with.

For Linux I’m sure a similar solution exists (probably something -l -i |ke –this| or > similar &) and Apple people always have their nicely designed shareware software with an ever awesome gui and candy-cute icons – though most of them aren’t aware that browsers other than Safari do exist.

jQuery slideDown jerkyness

Sunday, September 6th, 2009

I do have problems with jQuery effects: last time it was effects not finishing (okay launched 50+ backgroundCoor animation effect at once, but still) now it is the slideDown: on the very end of the animation it tends to skip the last 10-20 pixels.

One possible solution is to get the container’s height, save it into somewhere (if you’re lazy like me, then just add it as an attribute), set it to overflow hidden and animate to zero height. To slideDown, just animate back to the stored height:

$("#posts .postwrap").each(function(){ $(this).attr("height", $(this).height()); });
$("#posts .postwrap").css({overflow: "hidden"}).animate({height: 0}, 1000, callback);

Now, this is getting pretty ugly, yet we’re still not there; why? Because height() doesn’t work very well with margins, so instead of collapsing .post items, I had to add a .postwrap outer wrapper; now this works with the above (even though this trick doesn’t solve the issue with slideDown function) :(

Pure – javascript template

Tuesday, September 1st, 2009

Pure is an unobtrusive javascript templating engine: I gave it a try in a smaller pet project but it makes my tiny little brain hurt. I constantly feel that I have to act like the templater wants me to act: clean inputs, mess with the incoming jsons,  preformat data, do additional helping with jQuery, add sugar etc.

Probably it’s just my old fashioned brain, but I do like the usual templating toolset, and with the textarea trick or with some sort of prerendering it’s not that bad. Anyway, I’ll keep on trying: maybe I can get the hang of it.