Code Monkey home page Code Monkey logo

go-filecache's Introduction

go-filecache

GoDoc

Two-way filecache for golang for putting and getting immutable file from a file storage service.

Installation and Usage

The import path for the package is gopkg.in/hypirion/go-filecache.v1

To install it, run:

go get gopkg.in/hypirion/go-filecache.v1

To use it in your project, you import gopkg.in/hypirion/go-filecache.v1 and refer to it as filecache like this:

import "gopkg.in/hypirion/go-filecache.v1"

//...

filecache.DoStuff()

Quickstart

go-filecache is a small library that makes it possible to cache files/data in a temporary file. The cache is an LRU-cache where the total size is specified by the total size of the data, rather than the amount of entries.

For example, you might want to store user images on disk. But as your userbase grows, it turns out to be impossible to keep it all on disk, so you turn to Amazon S3. However, you still want to keep a portion of images on disk, as you may want to make thumbnails etc. of the images, or for other performance reasons.

With go-filecache, you can do this rather easily and thread-safely:

import (
    "net/http"
    // ...
    "gopkg.in/hypirion/go-filecache.v1"
    "gopkg.in/hypirion/go-filecache.v1/s3"
)

func main() {
    s3fs := s3.Filestore{
        BucketName: "mycorp",
        BucketPrefix: "images/",
        Region: "eu-central-1",
    }
    fcache := filecache.New(30 * filecache.GiB, s3fs)
    defer fcache.Close()
    // ...
}

//...
func sendImage(r *Resources, imageName string, w http.ResponseWriter) {
    if !hasAccess(r, imageName) {
        http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
        return
    }
    ok, err := r.S3Cache.Has(imagename)
    if err != nil {
        // handle error
    }
    if !ok {
        http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
        return
    }
    imgType, err := r.S3MetaCache.ContentType(imageName)
    if err != nil {
        // handle error.
    }
    w.Header().Set("Content-Type", "image/png")
    err = r.S3Cache.Get(w, imageName)
    if err != nil {
        // you know the drill
    }
}

What About Metadata?

go-filecache is purely for files, not the metadata around them. To retrieve metadata, you could put it inside the file, or you could make another cache which contains the metadata.

I would recommend using another cache for metadata. Although the caches will go out of sync, this shouldn't be a problem for semantics: go-filecache is, as mentioned used for immutable files, hence their (relevant) metadata should be immutable as well.

A third option would be to augment filecache.go to attach extra metadata to entries. In that fashion, you can extend Get/Put in the way you want, without feeling constrained. Documentation on how to do that is upcoming.

..But I Only Need Get!

You can implement Put/Has as a panic or constantly erroring out, just remind yourself to never use those functions :)

Dependencies

go-edn has no external dependencies, except the default Go library. The subpackage aws and awsversioned has an external dependency on aws-sdk-go, see their READMEs for more information.

License

Copyright © 2016 Jean Niklas L'orange

Distributed under the BSD 3-clause license, which is available in the file LICENSE.

go-filecache's People

Contributors

hypirion avatar

Stargazers

Dan Wolf avatar Peter Spiess-Knafl avatar  avatar yhiamdan avatar Timur Guseynov avatar Kevin Chen avatar Olivier Boukili avatar Felix Gürtler avatar  avatar Ian Rees avatar  avatar ‮ittaG ordnasselA avatar liuxiaobo avatar John Bolliger avatar Ewout van Mansom avatar  avatar

Watchers

 avatar James Cloos avatar  avatar wyxloading avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.