Created
March 18, 2012 13:51
-
-
Save nictuku/2073020 to your computer and use it in GitHub Desktop.
memcache read from file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"bytes" | |
"encoding/base64" | |
"flag" | |
"fmt" | |
"io" | |
"os" | |
"strings" | |
"github.com/bradfitz/gomemcache/memcache" | |
) | |
func main() { | |
flag.Parse() | |
path := flag.Arg(0) | |
mem := memcache.New("localhost:11211") | |
var ( | |
file *os.File | |
part []byte | |
prefix bool | |
err error | |
ih string | |
value []byte | |
) | |
if file, err = os.Open(path); err != nil { | |
fmt.Println(err) | |
return | |
} | |
reader := bufio.NewReader(file) | |
buffer := new(bytes.Buffer) | |
for { | |
if part, prefix, err = reader.ReadLine(); err != nil { | |
break | |
} | |
buffer.Write(part) | |
if !prefix { | |
s := strings.SplitN(buffer.String(), "|", 2) | |
ih = strings.ToLower(s[0]) | |
_, err := fmt.Sscanf(ih, "%x", &ih) | |
if err != nil { | |
fmt.Println("Could not read hex-encoded string", err) | |
} | |
ih = base64.StdEncoding.EncodeToString([]byte(ih)) | |
value = []byte(s[1]) | |
buffer.Reset() | |
} | |
if err = mem.Set(&memcache.Item{Key: ih, Value: value}); err != nil { | |
fmt.Printf("Error writing key '%q': %v\n", ih, err) | |
return | |
} else { | |
fmt.Println("wrote:", ih) | |
} | |
} | |
if err == io.EOF { | |
err = nil | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment