├── LICENSE ├── Makefile ├── README.md └── main.go /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Josh Baker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | lru-server: main.go 2 | go build -o lru-server 3 | clean: 4 | rm -f lru-server 5 | install: lru-server 6 | cp -f lru-server /usr/local/bin/lru-server 7 | uninstall: 8 | rm -f /usr/local/bin/lru-server 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `lru-server` 2 | 3 | A convenient [LRU cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)) that's built on [hashicorp/golang-lru](https://github.com/hashicorp/golang-lru) and [tidwall/modern-server](https://github.com/tidwall/modern-server). It uses a very simple HTTP REST api and supports Let's Encrypt. 4 | 5 | ## Building 6 | 7 | To start using lru-server, install Go and run: 8 | 9 | ```sh 10 | $ make 11 | ``` 12 | 13 | ## Using 14 | 15 | Start the server up. 16 | 17 | ```sh 18 | $ ./lru-server 19 | $ ./lru-server -s 100000 # specify an lru capacity of 100,000 20 | ``` 21 | 22 | ### Set a key 23 | 24 | ```sh 25 | $ curl -d "my value" localhost:8000/mykey 26 | ``` 27 | 28 | ### Get a key 29 | 30 | ```sh 31 | $ curl localhost:8000/mykey 32 | my value 33 | ``` 34 | 35 | ### Delete a key 36 | 37 | ```sh 38 | $ curl -X DELETE localhost:8000/mykey 39 | ``` 40 | 41 | ## Contact 42 | 43 | Josh Baker [@tidwall](http://twitter.com/tidwall) 44 | 45 | ## License 46 | 47 | `lru-server` source code is available under the MIT [License](/LICENSE). 48 | 49 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "io/ioutil" 6 | "net/http" 7 | "strings" 8 | 9 | "github.com/hashicorp/golang-lru" 10 | "github.com/tidwall/modern-server" 11 | ) 12 | 13 | func main() { 14 | var size int 15 | var cache *lru.Cache 16 | opts := &server.Options{ 17 | Version: "0.0.1", 18 | Name: "lru-server", 19 | Flags: func() { flag.IntVar(&size, "s", 1000000, "") }, 20 | FlagsParsed: func() { cache, _ = lru.New(size) }, 21 | Usage: func(s string) string { 22 | return strings.Replace(s, "{{USAGE}}", 23 | " -s size : size of lru (default: 1000000)\n", -1) 24 | }, 25 | } 26 | server.Main( 27 | func(w http.ResponseWriter, r *http.Request) { 28 | key := strings.Split(r.URL.Path, "/")[1] 29 | switch r.Method { 30 | case "GET": 31 | if val, ok := cache.Get(key); !ok { 32 | w.WriteHeader(404) 33 | } else { 34 | w.Write([]byte(val.(string))) 35 | } 36 | case "DELETE": 37 | cache.Remove(key) 38 | case "PUT", "POST": 39 | if data, err := ioutil.ReadAll(r.Body); err != nil { 40 | w.WriteHeader(500) 41 | } else { 42 | cache.Add(key, string(data)) 43 | } 44 | default: 45 | w.WriteHeader(405) 46 | } 47 | }, opts, 48 | ) 49 | } 50 | --------------------------------------------------------------------------------