Skip to content

Commit ca1817d

Browse files
committed
docs(caching): Separate README and EXAMPLES files for caching package (#103)
1 parent aa5e296 commit ca1817d

File tree

5 files changed

+100
-104
lines changed

5 files changed

+100
-104
lines changed

EXAMPLES.md

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -20,76 +20,4 @@ This document provides practical examples of how to use the library's features.
2020
14. [Time](/time/EXAMPLES.md)
2121
15. [Logging](/logging/EXAMPLES.md)
2222
16. [File System Utilities](/fsutils/EXAMPLES.md)
23-
17. [Caching](#15-caching)
24-
25-
# 17. Caching
26-
27-
## `CacheWrapper`
28-
29-
### A non-thread-safe caching decorator
30-
31-
```go
32-
package main
33-
34-
import (
35-
"fmt"
36-
"math/big"
37-
"github.com/kashifkhan0771/utils/math"
38-
)
39-
40-
// Example function: Compute factorial
41-
func factorial(n int) *big.Int {
42-
result := big.NewInt(1)
43-
for i := 2; i <= n; i++ {
44-
result.Mul(result, big.NewInt(int64(i)))
45-
}
46-
return result
47-
}
48-
49-
func main() {
50-
cachedFactorial := utils.CacheWrapper(factorial)
51-
fmt.Println(cachedFactorial(10))
52-
}
53-
```
54-
55-
#### Output:
56-
57-
```
58-
3628800
59-
```
60-
61-
---
62-
63-
## SafeCacheWrapper
64-
65-
### A thread-safe caching decorator
66-
67-
```go
68-
package main
69-
70-
import (
71-
"fmt"
72-
"math/big"
73-
"github.com/kashifkhan0771/utils/math"
74-
)
75-
76-
// Example function: Compute factorial
77-
func factorial(n int) *big.Int {
78-
result := big.NewInt(1)
79-
for i := 2; i <= n; i++ {
80-
result.Mul(result, big.NewInt(int64(i)))
81-
}
82-
return result
83-
}
84-
85-
func main() {
86-
cachedFactorial := utils.SafeCacheWrapper(factorial)
87-
fmt.Println(cachedFactorial(10))
88-
}
89-
```
90-
91-
#### Output:
92-
93-
```
94-
3628800
95-
```
23+
17. [Caching](/caching/EXAMPLES.md)

README.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,22 @@ Here’s the completed table with descriptions, documentation, and examples for
4141
| Package Name | Description | Documentation | Examples |
4242
| ------------- | -------------------------------------------------- | ----------------------------- | --------------------------------- |
4343
| **boolean** | Utilities for boolean value checking and toggling | [README](boolean/README.md) | [EXAMPLES](boolean/EXAMPLES.md) |
44-
| **caching** | Cache management utilities | coming soon | coming soon |
44+
| **caching** | Cache management utilities | [README](caching/README.md) | [EXAMPLES](caching/EXAMPLES.md) |
4545
| **ctxutils** | Context utilities | [README](ctxutils/README.md) | [EXAMPLES](ctxutils/EXAMPLES.md) |
4646
| **errutils** | Error aggregation and management utilities | [README](errutils/README.md) | [EXAMPLES](errutils/EXAMPLES.md) |
47+
| **fake** | Fake data generation (UUIDs, addresses, dates) | [README](fake/README.md) | [EXAMPLES](fake/EXAMPLES.md) |
48+
| **fsutils** | File system utilities (size, metadata, comparison) | [README](fsutils/README.md) | [EXAMPLES](fsutils/EXAMPLES.md) |
49+
| **logging** | Flexible logging system for Golang | [README](logging/README.md) | [EXAMPLES](logging/EXAMPLES.md) |
4750
| **maps** | Utilities for state and metadata maps | [README](maps/README.md) | [EXAMPLES](maps/EXAMPLES.md) |
51+
| **math** | Mathematical utilities and helpers | [README](math/README.md) | [EXAMPLES](math/EXAMPLES.md) |
4852
| **pointers** | Helper functions for working with pointer values | [README](pointers/README.md) | [EXAMPLES](pointers/EXAMPLES.md) |
4953
| **rand** | Random number and string generation utilities | [README](rand/README.md) | [EXAMPLES](rand/EXAMPLES.md) |
5054
| **slice** | Slice manipulation and de-duplication utilities | [README](slice/README.md) | [EXAMPLES](slice/EXAMPLES.md) |
5155
| **strings** | String manipulation and encoding utilities | [README](strings/README.md) | [EXAMPLES](strings/EXAMPLES.md) |
5256
| **structs** | Struct comparison utilities | [README](structs/README.md) | [EXAMPLES](structs/EXAMPLES.md) |
5357
| **templates** | Template rendering utilities | [README](templates/README.md) | [EXAMPLES](templates/EXAMPLES.md) |
54-
| **url** | URL parsing and manipulation utilities | [README](url/README.md) | [EXAMPLES](url/EXAMPLES.md) |
55-
| **math** | Mathematical utilities and helpers | [README](math/README.md) | [EXAMPLES](math/EXAMPLES.md) |
56-
| **fake** | Fake data generation (UUIDs, addresses, dates) | [README](fake/README.md) | [EXAMPLES](fake/EXAMPLES.md) |
5758
| **timeutils** | Time and date manipulation utilities | [README](time/README.md) | [EXAMPLES](time/EXAMPLES.md) |
58-
| **logging** | Flexible logging system for Golang | [README](logging/README.md) | [EXAMPLES](logging/EXAMPLES.md) |
59-
| **fsutils** | File system utilities (size, metadata, comparison) | [README](fsutils/README.md) | [EXAMPLES](fsutils/EXAMPLES.md) |
60-
61-
## Examples:
62-
63-
For examples of each function, please checkout [EXAMPLES.md](/EXAMPLES.md)
64-
65-
---
59+
| **url** | URL parsing and manipulation utilities | [README](url/README.md) | [EXAMPLES](url/EXAMPLES.md) |
6660

6761
# Contributions
6862

caching/EXAMPLES.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Caching Function Examples
2+
3+
## `CacheWrapper`
4+
5+
### A non-thread-safe caching decorator
6+
7+
```go
8+
package main
9+
10+
import (
11+
"fmt"
12+
"math/big"
13+
"github.com/kashifkhan0771/utils/math"
14+
)
15+
16+
// Example function: Compute factorial
17+
func factorial(n int) *big.Int {
18+
result := big.NewInt(1)
19+
for i := 2; i <= n; i++ {
20+
result.Mul(result, big.NewInt(int64(i)))
21+
}
22+
return result
23+
}
24+
25+
func main() {
26+
cachedFactorial := utils.CacheWrapper(factorial)
27+
fmt.Println(cachedFactorial(10))
28+
}
29+
```
30+
31+
#### Output:
32+
33+
```
34+
3628800
35+
```
36+
37+
---
38+
39+
## SafeCacheWrapper
40+
41+
### A thread-safe caching decorator
42+
43+
```go
44+
package main
45+
46+
import (
47+
"fmt"
48+
"math/big"
49+
"github.com/kashifkhan0771/utils/math"
50+
)
51+
52+
// Example function: Compute factorial
53+
func factorial(n int) *big.Int {
54+
result := big.NewInt(1)
55+
for i := 2; i <= n; i++ {
56+
result.Mul(result, big.NewInt(int64(i)))
57+
}
58+
return result
59+
}
60+
61+
func main() {
62+
cachedFactorial := utils.SafeCacheWrapper(factorial)
63+
fmt.Println(cachedFactorial(10))
64+
}
65+
```
66+
67+
#### Output:
68+
69+
```
70+
3628800
71+
```

caching/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### Caching
2+
3+
The caching package provides utilities for creating caching decorators to enhance the performance of functions by storing computed results. It includes both thread-safe and non-thread-safe implementations.
4+
5+
- **SafeCacheWrapper**: A thread-safe caching decorator that safely memoizes function results in concurrent environments.
6+
7+
- Uses `sync.Map` to ensure thread-safety
8+
- Caches all results indefinitely (no eviction)
9+
- Best suited for pure functions with limited input domains
10+
- Safe for concurrent access but may impact performance under high contention
11+
12+
- **CacheWrapper**: A non-thread-safe caching decorator that memoizes function results.
13+
- Caches all results indefinitely (no eviction)
14+
- Best suited for pure functions with limited input domains
15+
- Not safe for concurrent access
16+
- Use SafeCacheWrapper for concurrent scenarios
17+
18+
## Examples:
19+
20+
For examples of each function, please checkout [EXAMPLES.md](/caching/EXAMPLES.md)
21+
22+
---

fsutils/README.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,6 @@
1212

1313
- **GetFileMetadata**: Retrieves metadata for a specified file path. Returns a `FileMetadata` struct that can be marshaled to JSON.
1414

15-
### 17. Caching
16-
17-
The caching package provides utilities for creating caching decorators to enhance the performance of functions by storing computed results. It includes both thread-safe and non-thread-safe implementations.
18-
19-
- **SafeCacheWrapper**: A thread-safe caching decorator that safely memoizes function results in concurrent environments.
20-
21-
- Uses `sync.Map` to ensure thread-safety
22-
- Caches all results indefinitely (no eviction)
23-
- Best suited for pure functions with limited input domains
24-
- Safe for concurrent access but may impact performance under high contention
25-
26-
- **CacheWrapper**: A non-thread-safe caching decorator that memoizes function results.
27-
- Caches all results indefinitely (no eviction)
28-
- Best suited for pure functions with limited input domains
29-
- Not safe for concurrent access
30-
- Use SafeCacheWrapper for concurrent scenarios
31-
32-
---
33-
3415
## Examples:
3516

3617
For examples of each function, please checkout [EXAMPLES.md](/fsutils/EXAMPLES.md)

0 commit comments

Comments
 (0)