Skip to content

Instantly share code, notes, and snippets.

View juntaki's full-sized avatar

Jumpei Takiyasu juntaki

View GitHub Profile
package main
import (
"fmt"
)
func main() {
add := make(chan int)
sum := 0
go func() {
func (c *Cache) Set(i *Item) error {
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
err := enc.Encode(i)
if err != nil {
return err
}
err = c.client.Set(i.ID, buf.Bytes(), 0).Err()
if err != nil {
return err
func (c *Cache) Get(id string) (*Item, error) {
b, err := c.client.Get(id).Bytes()
if err == nil || err != redis.Nil {
buf := bytes.NewBuffer(b)
dec := msgpack.NewDecoder(buf)
var i Item
err = dec.Decode(&i)
if err == nil {
return &i,nil
}
type Cache struct {
Repository
}
func (c *Cache) Get(id string) (*Item, error) {
return c.Repository.Get(id)
}
func (c *Cache) Set(i *Item) error {
return c.Repository.Set(i)
type Item struct {
ID string
Val string
}
type Repository interface {
Get(id string) (*Item, error)
Set(*Item) error
Ping() bool
}
@juntaki
juntaki / nethttp.go
Last active December 25, 2019 10:35
GoのMiddlewareの書き方・透過的なキャッシュをつくる
package main
import (
"log"
"net/http"
)
func LogMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
package {{.Models}}
{{$ilen := len .Imports}}
{{if gt $ilen 0}}
import (
{{range .Imports}}"{{.}}"{{end}}
)
{{end}}
{{range .Tables}}
#!/bin/bash
inotifywait -q -m -e close_write articles/*.re |
while read -r filename event; do
./build-in-docker.sh
done
@juntaki
juntaki / PropTypes.js
Created February 27, 2017 16:44
Reduxをソースコードから理解する その2 ref: http://qiita.com/juntaki/items/7df6a6cbca990281ced8
export const storeShape = PropTypes.shape({
subscribe: PropTypes.func.isRequired,
dispatch: PropTypes.func.isRequired,
getState: PropTypes.func.isRequired
})
@juntaki
juntaki / Reducer
Last active February 28, 2017 00:07
Reduxをソースコードから理解する その1 ref: http://qiita.com/juntaki/items/d7b44fd9c2c35ea9ce24
const reducer = (state = initialState, action) => {
switch (action.type) {
case UPDATE:
// update state
return newState;
default:
return state;
}
};