Tessellationgaba

Very intense 2 months coding marathon to bring into the world the new version of Mandalagaba.

I completely rewrote the symmetry engine to be universal. When I coded the first version, I only wanted to scratch a specific radial symmetry itch and had to expand on narrowly conceived code to accommodate for features that came up from the tool’s success. With this new version, I instead gave myself a broad framework built for expansion, I can translate any penstroke at any angle in any location. Beyond mandalas, it makes possible tessellations and even the 2 combined.

I used the opportunity to add many features which were lacking: zooming, forking, lines, color picker, et cetera. With many more to come. The interface was rethought to be more accessible. Doing so took much more time than building the core engine.

There is an obscene amount of math that goes behind every pen stroke you draw in the tool. It was kind of fun to go through it again in my life, 20 years later. Even though I had forgotten about it all, it came back nicely. It’s amazing to have the internet as a tool to look up methods, to be able to describe the problem in plain English and have potential solutions thrown at you. It used to be that you needed to know what you needed precisely to find it in a book.

 

I love that Robin copies what I do no matter the understanding level, we’ve had lots of talks about what is going on.

It’s not just the math but also algorithms, languages & infrastructure. Not to toot my own horn but in my 30s, I’ve never felt so intimate with every aspect of an idea’s implementation. It’s extremely enabling to know exactly where to go to achieve X. Honestly though I’m a little burnt out at the moment, something that was supposed to take 10 days took more than 2 months of coding every single night.

My hope is that the new tool becomes a reference online for this type of work. And it’s all 100% free; well… we’ll talk about that in the next post.

Lower tech fun found in a thrift shop

A Universal Caching Algorithm for PHP using Memcached

Here is an elegant way to use the same caching logic for all function calls which should have a cache. With the proliferation of 3rd party APIs I was quite happy to find a way to address them all with a single mechanism.

function expensive_third_party_call( $param1, $param2 ) {
    // universal caching algorithm header
    $result = memcached_retrieve( __FUNCTION__ . serialize(func_get_args()) ) ;
    if( $result!==null ) {
        return $result ;
    }

    // this is where the third party call actually happens, if we are hit the cache missed
    $to_return = /* some super complex and time consuming logic, throw in a couple of web calls*/ ;

    // universal caching algorithm footer
    memcached_store( __FUNCTION__ . serialize(func_get_args()), $to_return, CACHE_TIMEOUT ) ;
    return $to_return ;
}



////////// helper functions bellow //////////
$m = false ;

function memcached_retrieve( $key ) {
    global $m ;

    $new_key = md5( $key ) ;
    if( $m===false ) {
        $m = new Memcached() ;
        $m->addServer( 'localhost', 11211 ) ;
    }
    $temp = $m->get( $new_key ) ;
    $result_code = $m->getResultCode() ;
    if( $result_code==Memcached::RES_SUCCESS ) {
        return $temp ;
    } else if( $result_code==Memcached::RES_NOTFOUND ) {
        return null ;
    } else {
        echo "error: can't retrieve memcached key {$key} with result_code {$result_code}" ;
    }

    return null ;
}



function memcached_store( $key, $data, $timeout ) {
    global $m ;

    $new_key = md5( $key ) ;

    if( $m===false ) {
        $m = new Memcached() ;
        $m->addServer( 'localhost', 11211 ) ;
    }
    
    // a little heavy handed but we use null to represent that nothing was found in the cache so we can't have this be the data
    if( $data===null ) {
        $data = false ;
    }

    $m->set( $new_key, $data, $timeout ) ;
    $result_code = $m->getResultCode() ;
    if( $result_code!==Memcached::RES_SUCCESS ) {
        echo "error: can't store memcached key {$key} with result_code {$result_code}" ;
        return false ;
    } else {
        return true ;
    }

    return false ;
}

Requirements

  • apt-get install memcached php-memcached
  • make sure to define CACHE_TIMEOUT

Functioning principle

Using PHP’s awareness of the current function it is in along with the parameters which are passed to it, we derive a unique key which is used so store and retrieve from the cache.