@bejibun/cache 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +88 -0
- package/builders/CacheBuilder.d.ts +13 -0
- package/builders/CacheBuilder.js +127 -0
- package/config/cache.d.ts +2 -0
- package/config/cache.js +12 -0
- package/configure.d.ts +1 -0
- package/configure.js +14 -0
- package/exceptions/CacheException.d.ts +4 -0
- package/exceptions/CacheException.js +14 -0
- package/exceptions/index.d.ts +1 -0
- package/exceptions/index.js +1 -0
- package/facades/Cache.d.ts +8 -0
- package/facades/Cache.js +21 -0
- package/facades/index.d.ts +1 -0
- package/facades/index.js +1 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/package.json +43 -0
- package/tsconfig.json +17 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
All notable changes to this project will be documented in this file.
|
|
3
|
+
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
## [v0.1.0](https://github.com/crenata/bejibun-cache/compare/v0.1.0...v0.1.0) - 2025-11-09
|
|
7
|
+
|
|
8
|
+
### 🩹 Fixes
|
|
9
|
+
|
|
10
|
+
### 📖 Changes
|
|
11
|
+
What's New :
|
|
12
|
+
Cache with Redis, currently only redis.
|
|
13
|
+
|
|
14
|
+
- `.remember()` Fetch data from cache if exists
|
|
15
|
+
- `.has()` Check if cache exists
|
|
16
|
+
- `.get()` Fetch data from cache
|
|
17
|
+
- `.add()` Insert data to cache, will return false if cache is already exists
|
|
18
|
+
- `.put()` Update cache data
|
|
19
|
+
- `.forget()` Delete cache
|
|
20
|
+
|
|
21
|
+
### ❤️Contributors
|
|
22
|
+
- Havea Crenata ([@crenata](https://github.com/crenata))
|
|
23
|
+
|
|
24
|
+
**Full Changelog**: https://github.com/crenata/bejibun-cache/blob/master/CHANGELOG.md
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Havea Crenata
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<img src="https://github.com/crenata/bejibun/blob/master/public/images/bejibun.png?raw=true" width="150" alt="Bejibun" />
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
# Cache for Bejibun
|
|
14
|
+
Cache for Bejibun Framework.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
### Installation
|
|
19
|
+
Install the package.
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# Using Bun
|
|
23
|
+
bun add @bejibun/cache
|
|
24
|
+
|
|
25
|
+
# Using Bejibun
|
|
26
|
+
bun ace install @bejibun/cache
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Configuration
|
|
30
|
+
The configuration file automatically executed if you are using `ace`.
|
|
31
|
+
|
|
32
|
+
Or
|
|
33
|
+
|
|
34
|
+
Add `cache.ts` inside config directory on your project if doesn't exist.
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
config/cache.ts
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
const config: Record<string, any> = {
|
|
42
|
+
connection: "redis",
|
|
43
|
+
|
|
44
|
+
connections: {
|
|
45
|
+
redis: {
|
|
46
|
+
host: "127.0.0.100",
|
|
47
|
+
port: 6379,
|
|
48
|
+
password: "",
|
|
49
|
+
database: 0
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export default config;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
You can pass the value with environment variables.
|
|
58
|
+
|
|
59
|
+
### How to Use
|
|
60
|
+
How to use tha package.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import Cache from "@bejibun/cache";
|
|
64
|
+
|
|
65
|
+
await Cache.remember("key", () => {}); // any
|
|
66
|
+
await Cache.has("key"); // boolean
|
|
67
|
+
await Cache.get("key"); // any
|
|
68
|
+
await Cache.add("key", "Hello world"); // boolean
|
|
69
|
+
await Cache.put("key", "Lorem ipsum"); // boolean
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Contributors
|
|
73
|
+
- [Havea Crenata](mailto:havea.crenata@gmail.com)
|
|
74
|
+
|
|
75
|
+
## ☕ Support / Donate
|
|
76
|
+
|
|
77
|
+
If you find this project helpful and want to support it, you can donate via PayPal :
|
|
78
|
+
|
|
79
|
+
[](https://paypal.me/hafiizhghulam)
|
|
80
|
+
|
|
81
|
+
Or if you are prefer using crypto :
|
|
82
|
+
|
|
83
|
+
| EVM | Solana |
|
|
84
|
+
| ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
|
|
85
|
+
| <img src="https://github.com/crenata/bejibun/blob/master/public/images/EVM.png?raw=true" width="150" /> | <img src="https://github.com/crenata/bejibun/blob/master/public/images/SOL.png?raw=true" width="150" /> |
|
|
86
|
+
| 0xdABe8750061410D35cE52EB2a418c8cB004788B3 | GAnoyvy9p3QFyxikWDh9hA3fmSk2uiPLNWyQ579cckMn |
|
|
87
|
+
|
|
88
|
+
Or you can buy this `$BJBN (Bejibun)` tokens [here](https://pump.fun/coin/CQhbNnCGKfDaKXt8uE61i5DrBYJV7NPsCDD9vQgypump), beware of bots.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export default class CacheBuilder {
|
|
2
|
+
protected conf: Record<string, any>;
|
|
3
|
+
protected prefix: string;
|
|
4
|
+
constructor();
|
|
5
|
+
private get config();
|
|
6
|
+
private key;
|
|
7
|
+
remember(key: string, callback: Function): Promise<any>;
|
|
8
|
+
has(key: string): Promise<boolean>;
|
|
9
|
+
get(key: string): Promise<any>;
|
|
10
|
+
add(key: string, value: any): Promise<boolean>;
|
|
11
|
+
put(key: string, value: any): Promise<boolean>;
|
|
12
|
+
forget(key: string): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import App from "@bejibun/app";
|
|
2
|
+
import Logger from "@bejibun/logger";
|
|
3
|
+
import Redis from "@bejibun/redis";
|
|
4
|
+
import { isEmpty, isNotEmpty } from "@bejibun/utils";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import CacheConfig from "../config/cache";
|
|
7
|
+
import CacheException from "../exceptions/CacheException";
|
|
8
|
+
export default class CacheBuilder {
|
|
9
|
+
conf;
|
|
10
|
+
prefix;
|
|
11
|
+
constructor() {
|
|
12
|
+
const configPath = App.Path.configPath("cache.ts");
|
|
13
|
+
let config;
|
|
14
|
+
if (fs.existsSync(configPath))
|
|
15
|
+
config = require(configPath).default;
|
|
16
|
+
else
|
|
17
|
+
config = CacheConfig;
|
|
18
|
+
this.conf = config;
|
|
19
|
+
this.prefix = "bejibun-cache";
|
|
20
|
+
}
|
|
21
|
+
get config() {
|
|
22
|
+
if (isEmpty(this.conf))
|
|
23
|
+
throw new CacheException("There is no config provided.");
|
|
24
|
+
return this.conf;
|
|
25
|
+
}
|
|
26
|
+
key(key) {
|
|
27
|
+
return `${this.prefix}/${key}`;
|
|
28
|
+
}
|
|
29
|
+
async remember(key, callback) {
|
|
30
|
+
let data;
|
|
31
|
+
switch (this.config.connection) {
|
|
32
|
+
case "redis":
|
|
33
|
+
data = await Redis.get(this.key(key));
|
|
34
|
+
if (isEmpty(data)) {
|
|
35
|
+
data = callback();
|
|
36
|
+
await Redis.set(this.key(key), data);
|
|
37
|
+
}
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
data = null;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
async has(key) {
|
|
46
|
+
let data;
|
|
47
|
+
switch (this.config.connection) {
|
|
48
|
+
case "redis":
|
|
49
|
+
data = await Redis.get(this.key(key));
|
|
50
|
+
break;
|
|
51
|
+
default:
|
|
52
|
+
data = false;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
return isNotEmpty(data);
|
|
56
|
+
}
|
|
57
|
+
async get(key) {
|
|
58
|
+
let data;
|
|
59
|
+
switch (this.config.connection) {
|
|
60
|
+
case "redis":
|
|
61
|
+
data = await Redis.get(this.key(key));
|
|
62
|
+
break;
|
|
63
|
+
default:
|
|
64
|
+
data = false;
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
return data;
|
|
68
|
+
}
|
|
69
|
+
async add(key, value) {
|
|
70
|
+
let status = true;
|
|
71
|
+
let data;
|
|
72
|
+
try {
|
|
73
|
+
switch (this.config.connection) {
|
|
74
|
+
case "redis":
|
|
75
|
+
data = await Redis.get(this.key(key));
|
|
76
|
+
break;
|
|
77
|
+
default:
|
|
78
|
+
data = null;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
if (isEmpty(data)) {
|
|
82
|
+
switch (this.config.connection) {
|
|
83
|
+
case "redis":
|
|
84
|
+
await Redis.set(this.key(key), value);
|
|
85
|
+
break;
|
|
86
|
+
default:
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
status = false;
|
|
92
|
+
Logger.setContext("Cache").info("The cache key is already exists.");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
status = false;
|
|
97
|
+
Logger.setContext("Cache").error("Failed to add cache.").trace(error);
|
|
98
|
+
}
|
|
99
|
+
return status;
|
|
100
|
+
}
|
|
101
|
+
async put(key, value) {
|
|
102
|
+
let status = true;
|
|
103
|
+
try {
|
|
104
|
+
switch (this.config.connection) {
|
|
105
|
+
case "redis":
|
|
106
|
+
await Redis.set(this.key(key), value);
|
|
107
|
+
break;
|
|
108
|
+
default:
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
status = false;
|
|
114
|
+
Logger.setContext("Cache").error("Failed to add cache.").trace(error);
|
|
115
|
+
}
|
|
116
|
+
return status;
|
|
117
|
+
}
|
|
118
|
+
async forget(key) {
|
|
119
|
+
switch (this.config.connection) {
|
|
120
|
+
case "redis":
|
|
121
|
+
await Redis.del(this.key(key));
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
package/config/cache.js
ADDED
package/configure.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/configure.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import App from "@bejibun/app";
|
|
2
|
+
import Logger from "@bejibun/logger";
|
|
3
|
+
import path from "path";
|
|
4
|
+
const configPath = path.resolve(__dirname, "config");
|
|
5
|
+
const regex = /\.(m?js|ts)$/;
|
|
6
|
+
const configs = Array.from(new Bun.Glob("**/*").scanSync({
|
|
7
|
+
cwd: configPath
|
|
8
|
+
})).filter(value => (regex.test(value) &&
|
|
9
|
+
!value.endsWith(".d.ts")));
|
|
10
|
+
for (const config of configs) {
|
|
11
|
+
const destination = config.replace(regex, ".ts");
|
|
12
|
+
await Bun.write(App.Path.configPath(destination), await Bun.file(path.resolve(configPath, config)).text());
|
|
13
|
+
Logger.setContext("CONFIGURE").info(`Copying ${config} into config/${destination}`);
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Logger from "@bejibun/logger";
|
|
2
|
+
import { defineValue } from "@bejibun/utils";
|
|
3
|
+
export default class CacheException extends Error {
|
|
4
|
+
code;
|
|
5
|
+
constructor(message, code) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "CacheException";
|
|
8
|
+
this.code = defineValue(code, 503);
|
|
9
|
+
Logger.setContext(this.name).error(this.message).trace(this.stack);
|
|
10
|
+
if (Error.captureStackTrace) {
|
|
11
|
+
Error.captureStackTrace(this, CacheException);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../exceptions/CacheException";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../exceptions/CacheException";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export default class Cache {
|
|
2
|
+
static remember(key: string, callback: Function): Promise<any>;
|
|
3
|
+
static has(key: string): Promise<boolean>;
|
|
4
|
+
static get(key: string): Promise<any>;
|
|
5
|
+
static add(key: string, value: string): Promise<boolean>;
|
|
6
|
+
static put(key: string, value: string): Promise<boolean>;
|
|
7
|
+
static forget(key: string): Promise<void>;
|
|
8
|
+
}
|
package/facades/Cache.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import CacheBuilder from "../builders/CacheBuilder";
|
|
2
|
+
export default class Cache {
|
|
3
|
+
static async remember(key, callback) {
|
|
4
|
+
return new CacheBuilder().remember(key, callback);
|
|
5
|
+
}
|
|
6
|
+
static async has(key) {
|
|
7
|
+
return new CacheBuilder().has(key);
|
|
8
|
+
}
|
|
9
|
+
static async get(key) {
|
|
10
|
+
return new CacheBuilder().get(key);
|
|
11
|
+
}
|
|
12
|
+
static async add(key, value) {
|
|
13
|
+
return new CacheBuilder().add(key, value);
|
|
14
|
+
}
|
|
15
|
+
static async put(key, value) {
|
|
16
|
+
return new CacheBuilder().put(key, value);
|
|
17
|
+
}
|
|
18
|
+
static async forget(key) {
|
|
19
|
+
return new CacheBuilder().forget(key);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../facades/Cache";
|
package/facades/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../facades/Cache";
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bejibun/cache",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"author": "Havea Crenata <havea.crenata@gmail.com>",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/crenata/bejibun-cache.git"
|
|
8
|
+
},
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"module": "index.js",
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@types/bun": "latest",
|
|
13
|
+
"tsc-alias": "^1.8.16"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/crenata/bejibun-cache/issues"
|
|
17
|
+
},
|
|
18
|
+
"description": "Cache for Bejibun Framework",
|
|
19
|
+
"homepage": "https://github.com/crenata/bejibun-cache#readme",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"bun",
|
|
22
|
+
"bun framework",
|
|
23
|
+
"cache",
|
|
24
|
+
"bejibun",
|
|
25
|
+
"typescript"
|
|
26
|
+
],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"alias": "bunx tsc-alias -p tsconfig.json",
|
|
30
|
+
"rsync": "rsync -a dist/ ./",
|
|
31
|
+
"clean": "rm -rf dist",
|
|
32
|
+
"build": "bunx tsc -p tsconfig.json && bun run alias && bun run rsync && bun run clean",
|
|
33
|
+
"deploy": "bun run build && bun publish --access public"
|
|
34
|
+
},
|
|
35
|
+
"type": "module",
|
|
36
|
+
"types": "index.d.ts",
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@bejibun/app": "^0.1.22",
|
|
39
|
+
"@bejibun/logger": "^0.1.22",
|
|
40
|
+
"@bejibun/redis": "^0.1.33",
|
|
41
|
+
"@bejibun/utils": "^0.1.20"
|
|
42
|
+
}
|
|
43
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "dist",
|
|
7
|
+
"rootDir": "src",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"moduleResolution": "bundler",
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"baseUrl": ".",
|
|
13
|
+
"paths": {
|
|
14
|
+
"@/*": ["src/*"]
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|