Skip to content

Commit

Permalink
Finished File Cache System
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaperskyguru committed May 19, 2021
1 parent 1b0568d commit 26f1cd5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
27 changes: 19 additions & 8 deletions src/Consumers/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import CacheInterface from '../Contracts/CacheInterface'
import ServiceInterface from '../Contracts/ServiceInterface'
class Cache implements CacheInterface {
private CacheService: ServiceInterface
private cacheService: ServiceInterface
constructor(cacheService: ServiceInterface) {
this.CacheService = cacheService
this.cacheService = cacheService
}

public async get(name: string): Promise<any> {
if (name) {
return await this.CacheService.get(name)
return await this.cacheService.get(name)
}
throw new Error('Specify a name')
}

public async has(name: string): Promise<Boolean> {
const value = await this.CacheService.get(name)
const value = await this.cacheService.get(name)
return !!value
}

public async set(name: string, data: any, minutes: number = 0): Promise<any> {
public async set(name: string, data: any, minutes: number): Promise<any> {
if (name && data) {
return await this.CacheService.set(name, data, minutes)
return await this.cacheService.set(name, data, minutes)
}
throw new Error('Specify a name and data to cache')
}

public async delete(name: string): Promise<Boolean> {
if (await this.has(name)) {
await this.CacheService.delete(name)
await this.cacheService.delete(name)
return true
}
return false
Expand Down Expand Up @@ -55,7 +55,7 @@ class Cache implements CacheInterface {
return await this.get(name)
} else {
const data = await callback()
await this.set(name, data)
await this.cacheService.set(name, data)
return data
}
}
Expand All @@ -75,6 +75,17 @@ class Cache implements CacheInterface {
}
return data
}

public async flush(): Promise<void> {
return await this.cacheService.flush()
}

public async forever(key: string, values: any): Promise<any> {
if (key && values) {
return await this.cacheService.set(key, values)
}
throw new Error('Specify a name and data to cache')
}
}

/*
Expand Down
3 changes: 2 additions & 1 deletion src/Contracts/CacheInterface.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
interface CacheInterface {
get(name: string): Promise<any>
set(name: string, data: any, duration: number): Promise<any>
update(name: string, data: any, duration: number): Promise<any>
forever(name: string, data: any): Promise<any>
update(name: string, data: any, duration?: number): Promise<any>
delete(name: string): Promise<Boolean>
remember(name: string, minutes: number, callback: Function): Promise<any>
rememberForever(name: string, callback: Function): Promise<any>
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/ServiceInterface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
interface ServiceInterface {
get(name: string): Promise<any>
set(name: string, data: any, duration: number): Promise<any>
set(name: string, data: any, duration?: number): Promise<any>
delete(name: string): Promise<Boolean>
flush(): Promise<void>
}
Expand Down
16 changes: 11 additions & 5 deletions src/Engines/FileCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,17 @@ class FileCache implements EngineInterface {
newArr['meta'] = { expiresIn }
newArr['data'] = data

fs.writeFileSync(this.path() + '/' + this.hashKey(key), JSON.stringify(newArr), 'binary')
fs.writeFileSync(
this.path() + '/' + this.hashKey(key) + '.cache',
JSON.stringify(newArr),
'binary',
)
}

private read(key: string): any {
const data = JSON.parse(fs.readFileSync(this.path() + '/' + this.hashKey(key), 'binary'))
const data = JSON.parse(
fs.readFileSync(this.path() + '/' + this.hashKey(key) + '.cache', 'binary'),
)

if (data['meta']) {
const time = new Date(data['meta']['expiresIn']).getTime()
Expand All @@ -80,11 +86,11 @@ class FileCache implements EngineInterface {

public async set(name: string, data: any, duration: number): Promise<any> {
if (!name) {
throw new Error('')
throw new Error('Cache key not provided')
}

if (!data) {
throw new Error('')
throw new Error('Cache data not provided')
}

if (duration !== 0) {
Expand All @@ -95,7 +101,7 @@ class FileCache implements EngineInterface {
}

public async flush(): Promise<void> {
throw new Error('Method not implemented.')
fs.rmdirSync(this.path(), { recursive: true })
}

public async delete(key: string): Promise<Boolean> {
Expand Down
4 changes: 4 additions & 0 deletions src/Services/FileCacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class FileCacheService extends FileCache implements ServiceInterface {
return false
}

public async flush(): Promise<void> {
return await super.flush()
}

public async update(name: string, data: any, duration: number): Promise<any> {
if (await this.delete(name)) {
return await this.set(name, data, duration)
Expand Down
2 changes: 1 addition & 1 deletion src/Services/RedisCacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RedisCacheService extends RedisCache implements ServiceInterface {
return !value
}

public async set(name: string, data: any, duration: number): Promise<any> {
public async set(name: string, data: any, duration: number = 0): Promise<any> {
if (name && data) {
return await super.set(name, this.serialize(data), duration)
}
Expand Down

0 comments on commit 26f1cd5

Please sign in to comment.