Skip to content

Instantly share code, notes, and snippets.

View Happytreat's full-sized avatar
🎯
Focusing

Melodies Sim Happytreat

🎯
Focusing
View GitHub Profile
# Credits to https://pdos.csail.mit.edu/6.824/notes/l-memcached.txt
k not in cache
C1 get(k), misses
C1 v1 = read k from DB
C2 writes k = v2 in DB
C2 delete(k)
C1 set(k, v1)
now mc has stale data, delete(k) has already happened
will stay stale indefinitely, until k is next written
@Happytreat
Happytreat / memaslap_install.txt
Last active February 13, 2021 15:21
Installing memaslap on Ubuntu 16.04
# Install libevent needed for memaslap
sudo apt-get install libevent-dev
cd libmemcached-1.0.18
# Edit Makefile by adding LDFLAGS "-L/lib64 -lpthread"
# https://bugs.launchpad.net/libmemcached/+bug/1562677
### diff of Makefile
# -LDFLAGS =
@Happytreat
Happytreat / conn_release.go
Created January 1, 2021 08:49
Reusable Errors
// condRelease releases this connection if the error pointed to by err
// is nil (not an error) or is predefined Reusable error
// The purpose is to not recycle TCP connections that are bad.
func (cn *conn) ConnRelease(err *error) {
if *err == nil || resumableError(*err) {
cn.release()
} else {
cn.nc.Close()
}
}
@Happytreat
Happytreat / connection_options.go
Created January 1, 2021 08:46
Connection Pool Options
type ConnectionOptions struct {
// The initial num of active connections when pool is initialised
// Pros: Warmup the connection pool with some connections on startup.
InitialActiveConnections int
// The maximum number of connections that can be active at any given
// time
MaxActiveConnections int
// The maximum number of idle connections per host that are kept alive by
@Happytreat
Happytreat / connection_pool.go
Last active January 1, 2021 07:39
Connection Pool Interface
type ConnectionPool interface {
// This gets an active connection from the connection pool if exists,
// else create a new connection.
Get() (net.Conn, error)
// This releases an active connection back to the connection pool.
Release(conn net.Conn) error
// This discards an active connection from the connection pool and
// closes the connection.
@Happytreat
Happytreat / BinarySearch.swift
Created May 13, 2020 04:56
lost type - binarySearch
/**
* Adapted Example from: Protocol-oriented Programming in Swift - WWDC 2015
* https://developer.apple.com/videos/play/wwdc2015/408/
*/
/**
* Without Self (protocol or class implementation)
* sortedKeys is a heterogeneous array of Ordered objects which could contain both Number and Label objects
*
* This implementation will raise an error if Self is being used:
@Happytreat
Happytreat / Ordered-withSelf.swift
Created May 13, 2020 04:33
lost type relationships - with Self
/**
* Example from: Protocol-oriented Programming in Swift - WWDC 2015
* https://developer.apple.com/videos/play/wwdc2015/408/
*/
protocol Ordered {
func precedes(other: Self) -> Bool
}
// value type struct
struct Number: Ordered {
@Happytreat
Happytreat / Ordered-withoutSelf.swift
Created May 13, 2020 04:21
lost type relationship - without Self
/**
* Example from: Protocol-oriented Programming in Swift - WWDC 2015
* https://developer.apple.com/videos/play/wwdc2015/408/
*/
class Ordered {
func precedes(other: Ordered) -> Bool { fatalError("implement me!") }
}
class Label : Ordered { var textL String = "" ... }
@Happytreat
Happytreat / Geometry.swift
Created May 13, 2020 03:54
no override protection example
/**
* Example from Protocol Oriented Programming - Advanced Swift Programming - raywenderlich.com
* https://www.youtube.com/watch?v=ekYdBcl3dzs
*/
protocol Geometry {
var size: CGSize { get }
func boundingBoxArea() -> CGFloat // area -> boundingBoxArea
}
@Happytreat
Happytreat / Shape.swift
Last active May 13, 2020 04:21
refactored protocol example
/**
* Adapted Example from Protocol Oriented Programming - Advanced Swift Programming - raywenderlich.com
* https://www.youtube.com/watch?v=ekYdBcl3dzs
*/
// Base class for inheritance
class Shape {
var size: CGSize
func draw(on context: CGContext) { fatalError("override \(#function)") }
func area() -> CGFloat { return size.width * size.height }