Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
mindplay-dk / Cache.php
Last active May 6, 2017 18:20
Simplied cache interface
<?php
namespace Psr\Cache;
/**
* Cache defines a common driver interface for interacting with a cache back-end.
*/
interface Driver
{
/**
@mindplay-dk
mindplay-dk / psr6.md
Last active May 6, 2017 18:20
PSR-6 cache interface review

I just learned about the proposed cache PSR, and I have to speak up.

I must apologize in advance, as my tone may get harsh or sarcastic, but it really upsets me to see a proposed standard which probably a lot of developers will end up following, to their detriment.

First off, FIG is the "Framework Interop Group" - did you actually look at other frameworks to see what they're doing? ZF2, Laravel, Doctrine, Symfony and Yii, to name a few of the major ones - not a single one of those use an aggregate model for individual cache items. How exactly is this standard supposed to help these frameworks interoperate?

For another, none of the cache providers you're going to be wrapping with this interface use an aggregate model - and for the most part this is probably deliberate, because the operations on a cache are supposed to be very focused and atomic, allowing you to optimize by doing as little work as possible.

This kind of thinking seems

@mindplay-dk
mindplay-dk / bench.php
Created July 19, 2014 09:16
Cut-and-paste benchmarking library with baseline compensation and weighted averages
<?php
header('Content-type: text/plain');
define('NUM_RUNS', 200); // total number of runs/readings
define('NUM_LOOP', 100); // number of iterations per run
// benchmark a function using various PHP features to establish
// a performance baseline for the PHP version / platform / etc.:
@mindplay-dk
mindplay-dk / rm_r.php
Last active February 14, 2025 06:31
Recursively delete a directory and all of it's contents - USE AT YOUR OWN RISK!
<?php
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
# http://stackoverflow.com/a/3352564/283851
/**
* Recursively delete a directory and all of it's contents - e.g.the equivalent of `rm -r` on the command-line.
@mindplay-dk
mindplay-dk / bench.js
Created March 22, 2014 04:36
JavaScript benchmark function
// noprotect
/**
* JavaScript benchmark function
*
* Usage: console.log(bench(function(){ ... }));
*/
bench = (function() {
var
@mindplay-dk
mindplay-dk / auth.php
Created March 12, 2014 21:30
Authorization service and context
<?php
/**
* Provides authorization services
*/
class AuthorizationService
{
/** @var AuthorizationRules */
public $rules;
@mindplay-dk
mindplay-dk / weighted_average.php
Created February 14, 2014 15:20
Computing weighted average from a set of values
function weighted_average($values) {
$sum = 0;
foreach ($values as $i => $value) {
$sum += $value;
}
$avg = $sum / count($values);
$error = array();
@mindplay-dk
mindplay-dk / view-proxy.php
Created January 29, 2014 22:52
Sort of like the presenter pattern, I guess - but the View can proxy any view-model type (no base class required) and can e.g. html-encode or filter and of it's properties... plus you get auto-completion and inspections via the type-hint on $view in an IDE...
<?php
/**
* This class acts as a proxy for arbitrary view-models
*/
class View
{
/**
* @var ViewModel raw view-model
*/
@mindplay-dk
mindplay-dk / ActiveRecord_createCommand.php
Created January 16, 2014 19:50
Invasive createCommand() method you can add to your own Yii ActiveRecord extension.
<?php
class ActiveRecord extends CActiveRecord
{
/**
* Create and configure a CDbCommand instance based on current criteria.
*
* Think twice before using this method - use only in cases where being able to stream
* through the raw SQL record-sets is crucial, usually for performance reasons, when
* dealing with very large record sets.
@mindplay-dk
mindplay-dk / GDistanceHelper.php
Created January 16, 2014 15:34
Helper class for Yii to build SQL queries that calculate the geographical distance from a point (in miles) - can be used with models that have latitude/longitude coordinates to filter by (and/or compute) the distance from a point.
<?php
/**
* @version 1.0
* @author Rasmus Schultz <http://blog.mindplay.dk/>
* @license LGPL3 <http://www.gnu.org/licenses/lgpl-3.0.txt>
*/
/**
* Helper-class to build SQL queries that calculate the geographical distance