Skip to content

Instantly share code, notes, and snippets.

Avatar

Brian Moon brianlmoon

View GitHub Profile
@brianlmoon
brianlmoon / pecl_memcached_quick.php
Created Apr 9, 2015
Quickest, safe way to add servers using pecl-memcached and persistent connections.
View pecl_memcached_quick.php
<?php
// Let's assume this is coming from a config file somewhere
// that we can trust to not have duplicate servers in the list.
$servers = array(
array("host" => 'localhost', "port" => 11211, "weight" => 10),
array("host" => 'localhost', "port" => 11212, "weight" => 20)
);
$mc = new Memcached("test");
@brianlmoon
brianlmoon / pecl_memcached_safe.php
Last active Aug 29, 2015
Safest way to add servers using pecl-memcached and persistent connections.
View pecl_memcached_safe.php
<?php
// Let's assume this is coming from a config file somewhere
// that maybe we can't trust, or could allow for someone to
// accidently add the same host/port twice.
$servers = array(
array("host" => 'localhost', "port" => 11211, "weight" => 10),
array("host" => 'localhost', "port" => 11212, "weight" => 20)
);
View example.php
<?php
if($whatever){
do_stuff();
} else {
$result["success"] = false;
$result["message"] = "FAILURE: Some error message";
}
return $result;
?>
@brianlmoon
brianlmoon / gist:1622095
Created Jan 16, 2012
Given two timestamps and a integer, returns the Y-m-d range, separated by days of that integer
View gist:1622095
<?php
/**
* Given two timestamps and a integer, returns the Y-m-d range, separated by days
* of that integer
*
* @param int $start_ts Starting timestamp
* @param int $end_ts Ending timestamp
* @return array
*
* @test
@brianlmoon
brianlmoon / gist:1622938
Created Jan 16, 2012
Get weeks from a range
View gist:1622938
<?php
/**
* Given two timestamps returns the Y-m-d ranges for each week within the range.
*
* @param int $start_ts Starting timestamp
* @param int $end_ts Ending timestamp
* @param bool $full_weeks If true, full weeks will be returned meaning
* the start and end will be adjusted to create full weeks
* @return array
@brianlmoon
brianlmoon / gist:1647760
Created Jan 20, 2012
Example output from running GearmanManager with do all workers
View gist:1647760
$ ./pecl-manager.php -c config-single.ini -w ./pecl-worker-classes -vvv
PID Type Message
25968 INFO Loading workers in ./pecl-worker-classes
25969 PROC Helper forked
25969 INFO Loading workers in ./pecl-worker-classes
25968 PROC Started with pid 25968
25970 WORKER Adding server 127.0.0.1
25970 WORKER Adding job Avg
25970 WORKER Adding job Sum
25968 PROC Started child 25970 (Avg,Sum)
@brianlmoon
brianlmoon / gearman_statsd.lua
Last active Oct 30, 2015
Sends stats about a gearmand process to StatsD
View gearman_statsd.lua
#!/usr/bin/lua
-- Requires lua 5.1+ and lua socket
--
-- Copyright (c) 2015, Brian Moon
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
--
-- * Redistributions of source code must retain the above copyright notice,
@brianlmoon
brianlmoon / gist:4962688
Last active Dec 13, 2015
Here is a function to return you the width the browser is using for media queries. For some reason, it is a bit different in each browser.
View gist:4962688
function getWindowMediaWidth() {
if(!window.matchMedia){
// it should be this, so return it when we can't
// figure it out. Of course, it does not do a lot
// of good if the browser does not support media
// queries.
return document.body.clientWidth;
}
@brianlmoon
brianlmoon / block_mac_nav.js
Created Jan 27, 2014
Block Mac navigation when form has changed
View block_mac_nav.js
function blockLeaving (e){
if(e.metaKey && (e.keyCode == 39 || e.keyCode == 37)){
switch(document.activeElement.tagName){
case "INPUT":
case "TEXTAREA":
break;
default:
if(!window.confirm("You have unsaved changes. Are you sure you want to leave the page?")){
e.preventDefault();
}
@brianlmoon
brianlmoon / current_on_object.php
Last active Apr 21, 2016
WTF PHP current() on object?
View current_on_object.php
<?php
class Foo {
private $bar = "private variable 1";
private $bar2 = "private variable 2";
}
$foo = new Foo;
echo current($foo)."\n";