Skip to content

Commit

Permalink
fc init
Browse files Browse the repository at this point in the history
  • Loading branch information
ucwong committed Feb 7, 2023
1 parent 87b9017 commit 855c2ce
Show file tree
Hide file tree
Showing 8 changed files with 1,245 additions and 0 deletions.
102 changes: 102 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
### VisualStudioCode template
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

# Local History for Visual Studio Code
.history/

### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

coverage.txt
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This Makefile is meant to be used by people that do not usually work
# with Go source code. If you know what GOPATH is then you probably
# don't need to bother with make.

.PHONY: all clean test
GOBIN = build/bin
OS = $(shell uname)
ifeq ($(OS), Linux)
endif

ifeq ($(OS), Darwin)
endif

format:
find . -name '*.go' -type f -not -path "./vendor*" -not -path "*.git*" -not -path "*/generated/*" | xargs gofmt -w -s
test:
go test ./... -v -race -cpu=1,2,4 -coverprofile=coverage.txt -covermode=atomic -benchmem -bench .
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# filecache
## a simple file cache of Golang

## Overview

A file cache can be created with either the `NewDefaultCache()` function to
get a cache with the defaults set, or `NewCache()` to get a new cache with
`0` values for everything; you will not be able to store items in this cache
until the values are changed; specifically, at a minimum, you should set
the `MaxItems` field to be > 0.

Let's start with a basic example; we'll create a basic cache and give it a
maximum item size of 128M:

```
cache := filecache.NewDefaultCache()
cache.MaxSize = 128 * filecache.Megabyte
cache.Start()
```

The `Kilobyte`, `Megabyte`, and `Gigabyte` constants are provided as a
convience when setting cache sizes.

When `cache.Start()` is called, a goroutine is launched in the background
that routinely checks the cache for expired items. The delay between
runs is specified as the number of seconds given by `cache.Every` ("every
`cache.Every` seconds, check for expired items"). There are three criteria
used to determine whether an item in the cache should be expired; they are:

1. Has the file been modified on disk? (The cache stores the last time
of modification at the time of caching, and compares that to the
file's current last modification time).
2. Has the file been in the cache for longer than the maximum allowed
time?
3. Is the cache at capacity? When a file is being cached, a check is
made to see if the cache is currently filled. If it is, the item that
was last accessed the longest ago is expired and the new item takes
its place. When loading items asynchronously, this check might miss
the fact that the cache will be at capacity; the background scanner
performs a check after its regular checks to ensure that the cache is
not at capacity.

The background scanner can be disabled by setting `cache.Every` to 0; if so,
cache expiration is only done when the cache is at capacity.

Once the cache is no longer needed, a call to `cache.Stop()` will close down
the channels and signal the background scanner that it should stop.


## Usage

### Initialisation and Startup

The public fields of the `FileCache` struct are:

```
MaxItems int // Maximum number of files to cache
MaxSize int64 // Maximum file size to store
ExpireItem int // Seconds a file should be cached for
Every int // Run an expiration check Every seconds
```

You can create a new file cache with one of two functions:

* `NewCache()`: creates a new bare repository that just has the underlying
cache structure initialised. The public fields are all set to `0`, which is
very likely not useful (at a minimum, a `MaxItems` of `0` means no items can
or will be stored in the cache).
* `NewDefaultCache()` returns a new file cache initialised to some basic
defaults. The defaults are:

```
DefaultExpireItem int = 300 // 5 minutes
DefaultMaxSize int64 = 4 * Megabyte
DefaultMaxItems int = 32
DefaultEvery int = 60 // 1 minute
```

These defaults are public variables, and you may change them to more useful
values to your program.

Once the cache has been initialised, it needs to be started using the
`Start()` method. This is important for initialising the goroutine responsible
for ensuring the cache remains updated, as well as setting up the asynchronous
caching goroutine. The `Active` method returns true if the cache is currently
running. `Start()` returns an `error` if an error occurs; if one is returned,
the cache should not be used.
Loading

0 comments on commit 855c2ce

Please sign in to comment.