Skip to content

Instantly share code, notes, and snippets.

@payden
payden / thing.php
Created July 15, 2014 15:57
Yet another thing
protected function _set_item_in_memcache($type, $id, $obj) {
$mc_key = "cache_{$type}_{$id}";
$this->cache->set($mc_key, json_encode($obj));
}
protected function _populate_item_from_memcache($type, $id) {
$mc_key = "cache_{$type}_{$id}";
$rv = $this->cache->get($mc_key);
if ($rv === false) return;
$this->_caches[$type][$id] = json_decode($rv, true);
@payden
payden / thing.php
Created July 15, 2014 15:02
2 layer caching.
protected function _get_contact_info($contact_id) {
//check local process cache first
if (!isset($this->_contact_info_cache[$contact_id])) {
$memcache_key = "contact_info_cache_" . $contact_id;
$memcache_res = $this->cache->get($memcache_key);
//then check memcache
if ($memcache_res) {
$this->_contact_info_cache[$contact_id] = json_decode($memcache_res, true);
} else {
//fall back to DB retrieval and store in both memcache and local process cache.
//
// OBFViewController.m
// Cryptsy Balance
//
// Created by Payden Sutherland on 3/9/14.
// Copyright (c) 2014 Payden Sutherland. All rights reserved.
//
#import "OBFViewController.h"
#import "CommonCrypto/CommonDigest.h"
@payden
payden / validity.c
Created December 31, 2013 02:28
Validity Sensors 138a:0050 testing code
/*
Hackity hack hack.
Don't judge me for this.
Clearly, this is for testing purposes only.
./gcc test.c -o test -lusb-1.0
*/
/**
* Generates the Directory Tree
*
* @class MODx.tree.Directory
* @extends MODx.tree.Tree
* @param {Object} config An object of options.
* @xtype modx-tree-directory
*/
MODx.tree.Directory = function (config) {
config = config || {};
int
onmessage(libwebsock_client_state *state, libwebsock_message *msg)
{
unsigned int count = 0;
unsigned int len = strlen(msg->payload);
libwebsock_client_state *current;
for (current = state->ctx->clients_HEAD; current != NULL; current = current->next) {
libwebsock_send_fragment(current, msg->payload, len, WS_FRAGMENT_FIN | WS_OPCODE_TEXT);
count++;
}
int
onmessage(libwebsock_client_state *state, libwebsock_message *msg)
{
//my message is in msg->payload
//let's send received message to all clients
return libwebsock_send_all_text(state->ctx, msg->payload);
}
struct _sapi_module_struct {
char *name;
char *pretty_name;
int (*startup)(struct _sapi_module_struct *sapi_module);
int (*shutdown)(struct _sapi_module_struct *sapi_module);
int (*activate)(TSRMLS_D);
int (*deactivate)(TSRMLS_D);
@payden
payden / php-ws.php
Last active December 29, 2015 23:19
php-ws example
<?php
$ws = new WebSocketServer();
$ws->bind("0.0.0.0", "8080");
$ws->onopen = function($client) {
echo "New connection: " . $client->sockfd . "\n";
};
$ws->onclose = function($client) {
echo "Closing connection: " . $client->sockfd . "\n";
};
$ws->onmessage = function($client, $msg) {
@payden
payden / gist:4553917
Created January 17, 2013 05:33
libwebsock-python gist
import libwebsock
def ws_onopen(client):
print "New connection from {0}".format(client.sock)
def ws_onclose(client)
print "Connection closed: {0}".format(client.sock)
def ws_onmessage(client, msg):
print "Received message from client ({0}): {1}".format(client.sock, msg.payload)