R
R
Roman Rakzin2015-07-18 19:44:15
go
Roman Rakzin, 2015-07-18 19:44:15

How to cache files in golang?

I want to cache certain files on upload so that they can be quickly returned when requested. As a result, I came up with a crutch code, so that when loading variables would be created and filled with values ​​from the corresponding files, and then the user, when accessed, would receive the corresponding variable. It turned out to be a rather long monotonous code (so far I haven’t even included it in the cycle for automation). As an option, I think to create a cycle that would take the name of the variable and the path to the file from the file, for example (or write it inside the generated file) and the variable will be created in the cycle and filled with the value from the file. Or is this not a good option? The question is whether there is already something ready to cache files.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
index0h, 2015-07-18
@index0h

If the direction is web, use the nginx file cache and you don't need to write it in go.
If you need it on go:
Create a structure with a private mapping (keys - file names, values ​​- their data) inside and RW mutex + perform locks in case of reading / writing. In this case, the recording should be performed at the time of application initialization.
----upd----

type MapInterface interface {
  Has(key string) bool
  Set(key string, data []byte)
  Get(key string) (bool, []byte)
  Remove(key string)
}

type Map struct {
  sync.RWMutex

  data map[string][]byte
}

See also https://github.com/golang/groupcache/blob/master/l...
C golang apparently too, you have nothing to lose))
x-sendfile to help))

R
Roman Rakzin, 2015-07-18
@TwoRS

C nginx didn't work. The point is that I do not want to give out this cached file to everyone. First, there is an authorization check, and then it is given - i.e. nginx does not know who can give and who can not. And you can give an example of your implementation of what is described in go, otherwise it is somehow very vague for me. I read that nginx gives the file the fastest, but is it worth running it and adding it to the general project, or is go also fast and the gain is small?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question