Skip to content

Commit 18146cc

Browse files
authored
refactor code for performance and security (#19)
2 parents a5b72e9 + 3060b7a commit 18146cc

File tree

7 files changed

+2314
-92
lines changed

7 files changed

+2314
-92
lines changed

.github/workflows/build.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
name: check
22
on:
3-
push:
43
pull_request:
4+
branches: [main]
5+
pull_request_target:
6+
types: [opened, synchronize, reopened]
7+
branches: [main]
58
workflow_dispatch:
69

710
jobs:
@@ -15,9 +18,9 @@ jobs:
1518
with:
1619
version: latest
1720
-
18-
uses: golangci/golangci-lint-action@v4
21+
uses: golangci/golangci-lint-action@v7
1922
with:
20-
version: latest
23+
version: v2.2.0
2124
-
2225
run: go test ./...
2326
shell: bash

.github/workflows/release.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@ on:
44
- '*'
55

66
name: "Create release"
7+
8+
permissions:
9+
contents: write
10+
711
jobs:
812
goreleaser:
913
runs-on: ubuntu-latest
1014
steps:
11-
-
12-
name: Checkout
15+
- name: Checkout
1316
uses: actions/checkout@v4
1417
with:
1518
fetch-depth: 0
16-
-
17-
uses: kevincobain2000/action-gobrew@v2
19+
- name: Set up Go
20+
uses: actions/setup-go@v5
1821
with:
19-
version: latest
20-
-
21-
name: Run GoReleaser
22-
uses: goreleaser/goreleaser-action@v5
22+
go-version: stable
23+
- name: Run GoReleaser
24+
uses: goreleaser/goreleaser-action@v6
2325
with:
2426
distribution: goreleaser
25-
version: latest
27+
version: "~> v2"
2628
args: release --clean
2729
env:
2830
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.golangci.yaml

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1+
version: "2"
12
linters:
2-
# Disable all linters.
3-
# Default: false
4-
disable-all: true
5-
# Enable specific linter
6-
# https://golangci-lint.run/usage/linters/#enabled-by-default
3+
default: none
74
enable:
8-
- errcheck
9-
- gosimple
10-
- govet
11-
- ineffassign
12-
- staticcheck
135
- dupl
6+
- errcheck
147
- errorlint
15-
- exportloopref
168
- goconst
179
- gocritic
1810
- gocyclo
1911
- goprintffuncname
2012
- gosec
13+
- govet
14+
- ineffassign
2115
- prealloc
2216
- revive
23-
- stylecheck
17+
- staticcheck
2418
- whitespace
19+
exclusions:
20+
generated: lax
21+
presets:
22+
- comments
23+
- common-false-positives
24+
- legacy
25+
- std-error-handling
26+
paths:
27+
- third_party$
28+
- builtin$
29+
- examples$
30+
formatters:
31+
exclusions:
32+
generated: lax
33+
paths:
34+
- third_party$
35+
- builtin$
36+
- examples$

.goreleaser.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
version: 2
2+
13
before:
24
hooks:
35
- go mod tidy
6+
47
builds:
5-
-
6-
env:
8+
- env:
79
- CGO_ENABLED=0
810
goos:
911
- linux
@@ -12,6 +14,11 @@ builds:
1214
goarch:
1315
- amd64
1416
- arm64
17+
ldflags:
18+
- -s -w
19+
- -X main.version={{.Version}}
20+
- -X main.commit={{.Commit}}
21+
- -X main.date={{.Date}}
1522
archives:
1623
-
1724
format: binary

cache_example.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// Example of how to use the improved directory cache with performance monitoring
9+
func ExampleCacheUsage() {
10+
// Create custom cache configuration
11+
cacheConfig := &CacheConfig{
12+
TTL: 2 * time.Minute, // Longer TTL for better performance
13+
CleanupInterval: 1 * time.Minute, // More frequent cleanup
14+
MaxEntries: 500, // Smaller cache for this example
15+
EnablePeriodicCleanup: true,
16+
}
17+
18+
// Create cache with custom configuration
19+
fs := &DefaultFileSystem{}
20+
cache := NewDirCache(cacheConfig, fs)
21+
defer cache.Close() // Important: close to stop cleanup goroutine
22+
23+
// Test some directory checks
24+
testDirs := []string{
25+
"/tmp",
26+
"/etc",
27+
"/var",
28+
"/usr/bin",
29+
"/nonexistent/dir",
30+
}
31+
32+
fmt.Println("Testing directory cache performance...")
33+
34+
// First round - cache misses
35+
start := time.Now()
36+
for _, dir := range testDirs {
37+
exists := cache.IsDirectoryNotEmpty(dir)
38+
fmt.Printf("Directory %s exists and not empty: %v\n", dir, exists)
39+
}
40+
firstRoundTime := time.Since(start)
41+
42+
// Second round - cache hits (should be much faster)
43+
start = time.Now()
44+
for _, dir := range testDirs {
45+
exists := cache.IsDirectoryNotEmpty(dir)
46+
fmt.Printf("Directory %s exists and not empty: %v (cached)\n", dir, exists)
47+
}
48+
secondRoundTime := time.Since(start)
49+
50+
// Get and display cache statistics
51+
stats := cache.GetStats()
52+
fmt.Printf("\n=== Cache Performance Statistics ===\n")
53+
fmt.Printf("Cache Hits: %d\n", stats.Hits)
54+
fmt.Printf("Cache Misses: %d\n", stats.Misses)
55+
fmt.Printf("Hit Ratio: %.2f%%\n", stats.HitRatio()*100)
56+
fmt.Printf("Total Entries: %d\n", stats.TotalSize)
57+
fmt.Printf("Evictions: %d\n", stats.Evictions)
58+
59+
fmt.Printf("\n=== Performance Improvement ===\n")
60+
fmt.Printf("First round (cache misses): %v\n", firstRoundTime)
61+
fmt.Printf("Second round (cache hits): %v\n", secondRoundTime)
62+
if secondRoundTime.Nanoseconds() > 0 {
63+
speedup := float64(firstRoundTime.Nanoseconds()) / float64(secondRoundTime.Nanoseconds())
64+
fmt.Printf("Speedup: %.1fx faster with cache\n", speedup)
65+
}
66+
}
67+
68+
// Uncomment to run the example:
69+
// func main() {
70+
// ExampleCacheUsage()
71+
// }

0 commit comments

Comments
 (0)