Skip to content

Instantly share code, notes, and snippets.

Josiah Carlson josiahcarlson

Block or report user

Report or block josiahcarlson

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@josiahcarlson
josiahcarlson / rom_example_bruno.py
Last active Aug 8, 2019
Example from a livecoding stream, updated with fixes
View rom_example_bruno.py
'''
This this was retyped from https://www.twitch.tv/videos/459202303 for notes and
then updated by the author of rom, me, Josiah Carlson
Notes:
* For created_at, or any default, if it is "callable", it will be called at
entity creation/instantiation. In the demo in the video, we see:
created_at = rom.DateTime(default=datetime.datetime.now())
View reply_to_blog.txt
This is my reply to Alex Mills: http://www.binpress.com/tutorial/introduction-to-rate-limiting-with-redis/155
Yes, with threads, processes, or an async reactor. That said...
$ redis-benchmark -n 2000000 -c 20 -q
[snip]
SET: 115141.05 requests per second
GET: 134048.27 requests per second
INCR: 180505.41 requests per second
LPUSH: 176056.33 requests per second
@josiahcarlson
josiahcarlson / search_output.txt
Last active Feb 14, 2016
A new ordering on an array that offers faster searching than binary search, yet is just as simple. Allows for O(log(n)) modifications and searches. Paper and details forthcoming.
View search_output.txt
$ gcc -O4 -o shuffle_search.out shuffle_search.c
$ ./shuffle_search.out
Generate 10000000 random input values...
Select 1000000 random items from the input and duplicating 10x times...
Random reorder selected items
Sorting input... qsort O(nlogn)
Shuffle transforming input... O(n)
Binary searching
3.932018
View redis-zadd-sum.lua
-- keys: key dest
-- argv: score member [score member ...]
-- e.g. keys: "data:test:1:2" "data-sum:test:1:2"
-- argv: 10 a 15 b
local key = KEYS[1]
local dest = KEYS[2]
local sum = tonumber(redis.call('get', dest)) or 0
for i=1, #ARGV, 2 do
@josiahcarlson
josiahcarlson / redis_slots.py
Created Nov 19, 2014
Some useful crc utility functions for Redis cluster
View redis_slots.py
'''
Released into the public domain.
This code will append 2 bytes to the end of a key to force the key to a specific Redis slot.
'''
from collections import defaultdict
from itertools import imap
import struct
@josiahcarlson
josiahcarlson / rate_limit2.py
Last active May 23, 2019
Regular and sliding window rate limiting to accompany two blog posts.
View rate_limit2.py
'''
rate_limit2.py
Copyright 2014, Josiah Carlson - josiah.carlson@gmail.com
Released under the MIT license
This module intends to show how to perform standard and sliding-window rate
limits as a companion to the two articles posted on Binpress entitled
"Introduction to rate limiting with Redis", parts 1 and 2:
@josiahcarlson
josiahcarlson / result_multiplex.py
Created Oct 12, 2014
Reliably multiplex receiving results via Redis lists
View result_multiplex.py
'''
result_multiplex.py
Copyright 2014 Josiah Carlson
Released as LGPL 2 or 3, your choice.
Problem:
You have a task processor that writes the results of tasks to a specified key
@josiahcarlson
josiahcarlson / rate_limit.py
Last active May 15, 2018
Rate limiting with Redis primarily using Lua scripting
View rate_limit.py
'''
rate_limit.py
Written May 7-8, 2014 by Josiah Carlson
Released under the MIT license.
Offers a simple interface for offering rate limiting on a per second, minute,
hour, and day basis. Useful for offering varying rate limits depending on user
behavior. Sliding window based limits offer per minute, hour, and day limits
@josiahcarlson
josiahcarlson / follow_file.py
Created Apr 13, 2014
Python function to offer "tail -F"-like functionality in Python
View follow_file.py
'''
follow_file.py
Written April 13, 2014 by Josiah Carlson
Released under the MIT license.
This could likely be made more efficient with the use of the pyinotify library,
and some of the module-level constants could be tuned for better performance,
depending on your platform.
@josiahcarlson
josiahcarlson / zunion_range_score.py
Last active Aug 29, 2015
Zunion Range Score, an interesting scripting solution.
View zunion_range_score.py
'''
Written on March 28, 2014 by Josiah Carlson
Released into the public domain
ZUNIONRANGESCORE:
Zunion Range Score performs a Redis ZUNIONSTORE operation, selecting *only
those items in the provided ranges. The scores are added. Proof of concept.
Warning: untested, use at your own risk.
You can’t perform that action at this time.