Giter Site home page Giter Site logo

filecache's Introduction

filecache (In-memory file caching using Go)

Build Status Sourcegraph GoDoc Minimal Version

About

This package recursively walks through a directory and caches files that match a regexp into a radix tree.

Since it spawns one goroutine for each file / directory lookup, it is also context-aware, enabling all the process to return earlier when the context is done.

Usage

Full documentation here.

Installing

Go 1.10

vgo get -u github.com/gbrlsnchs/filecache

Go 1.11 or after

go get -u github.com/gbrlsnchs/filecache

Importing

import (
	// ...

	"github.com/gbrlsnchs/filecache"
)

Reading all files in a directory

c, err := filecache.ReadDir("foobar", "")
if err != nil {
	// If err != nil, directory "foobar" doesn't exist, or maybe one of the files
	// inside this directory has been deleted during the reading.
}
txt := c.Get("bazqux.txt")
log.Print(txt)

Reading specific files in a directory

c, err := filecache.ReadDir("foobar", `\.sql$`)
if err != nil {
	// ...
}
q := c.Get("bazqux.sql")
log.Print(q)
log.Print(c.Len())  // amount of files cached
log.Print(c.Size()) // total size in bytes

Lazy-reading a directory

c := filecache.New("foobar")

// do stuff...

if err := c.Load(`\.log$`); err != nil {
	// ...
}

Setting a custom goroutine limit

By default, this package spawns goroutines for each file inside each directory.
Currently, the limit of goroutines is the result of runtime.NumCPU(). However, it is possible to use a cache with a custom limit by using the method SetSemaphoreSize.

c := filecache.New("foobar")
c.SetSemaphoreSize(100)
if err := c.Load(`\.log$`); err != nil {
	// ...
}

Contributing

How to help

filecache's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  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.