Giter Site home page Giter Site logo

ramdisk's Introduction

ramdisk

a RAM disk written in Go. a RAM disk is a file system where data is held only in RAM.

An alpha-stage RAM disk implemented in Go. The RAM disk can be mounted as a Linux file system in user space (FUSE), needing no elevated privileges. Files can be created, read and written, but are not persisted to durable storage. Sufficient current must be flowing all the time.

The Go process creating the RAM disk has direct in-process access to file data, represented by a byte slice.

prepare

 git clone https://github.com/berndfo/ramdisk.git
 cd ramdisk
 export GOPATH=`pwd` # use backticks here!
 go get bazil.org/fuse
 go get golang.org/x/net
 
 go run src/main.go

how to mount

Mounting a RAM disk is very simple. Just prepare the mount point (here: /mnt/myramdisk) and run:

	ramdisk.MountAndServe("/mnt/myramdisk", nil)

A mounted RAM disk can be accessed like any other file system on Linux (cd, cp, echo, cat, etc.). No byte will ever hit any disk. All data is lost after terminating the process.

how to track changes to FS

to act on changes in the in-process RAM disk, you can listen on a number of channels:

func main() {

	fsevents := ramdisk.NewFSEvents()

	go func() {
		for {
			var event interface{}
			select {
			case event = <-fsevents.FileCreated:
				log.Printf("file create: %q", event.(ramdisk.EventFileCreated).File.Meta.Name())
			case event = <-fsevents.FileOpened:
			case event = <-fsevents.FileWritten:
			case event = <-fsevents.FileClosed:
				file := event.(ramdisk.EventFileClosed)
				log.Printf("file closed: %q, size = %d", file.File.Meta.Name(), file.File.Meta.Size())
			case event = <-fsevents.Unmount:
			}
		}
	} ()

	ramdisk.MountAndServe("/mnt/myramdisk", &fsevents)
}

in this example, every file creation and close operation is logged. Please make sure to listen on all channels, but feel free to ignore any event you're not interested in.

how to unmount

# on the Linux shell
fusermount -u /mnt/fusemnt

or

// in Go code
fuse.Unmount(mountpoint)

accessing file data in-process

assume that latest is holding a recently written JPG image: var latest *ramdisk.FileEntry // last closed file entry this file might have been written by ffmpeg or any another out-of-process application. a web request can directly render this image to the response by copying data

func webHandler(response http.ResponseWriter, request *http.Request) {
    response.Header().Add("Content-type", "image/jpg")
    response.Write(latest.Data)
}

for a running, detailed example see src/ramdisk/webserver/main.go

missing features

-[ ] deletion of files -[ ] support directory structure

ramdisk's People

Contributors

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