Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file provider cache name & removed useless await #17

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
16 changes: 8 additions & 8 deletions src/Consumers/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Cache implements CacheInterface {

public async get(name: string): Promise<any> {
if (name) {
return await this.cacheService.get(name)
return this.cacheService.get(name)
}
throw new Error('Specify a name')
}
Expand All @@ -20,7 +20,7 @@ class Cache implements CacheInterface {

public async set(name: string, data: any, minutes: number): Promise<any> {
if (name && data) {
return await this.cacheService.set(name, data, minutes)
return this.cacheService.set(name, data, minutes)
}
throw new Error('Specify a name and data to cache')
}
Expand All @@ -36,13 +36,13 @@ class Cache implements CacheInterface {
public async update(name: string, data: any, minutes: number = 0): Promise<any> {
if (await this.has(name)) {
await this.delete(name)
return await this.set(name, data, minutes)
} else return await this.set(name, data, minutes)
return this.set(name, data, minutes)
} else return this.set(name, data, minutes)
}

public async remember(name: string, minutes: number, callback: Function): Promise<any> {
if (await this.has(name)) {
return await this.get(name)
return this.get(name)
} else {
const data = await callback()
await this.set(name, data, minutes)
Expand All @@ -52,7 +52,7 @@ class Cache implements CacheInterface {

public async rememberForever(name: string, callback: Function): Promise<any> {
if (await this.has(name)) {
return await this.get(name)
return this.get(name)
} else {
const data = await callback()
await this.cacheService.set(name, data)
Expand All @@ -77,12 +77,12 @@ class Cache implements CacheInterface {
}

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

public async forever(key: string, values: any): Promise<any> {
if (key && values) {
return await this.cacheService.set(key, values)
return this.cacheService.set(key, values)
}
throw new Error('Specify a name and data to cache')
}
Expand Down
18 changes: 9 additions & 9 deletions src/Engines/FileCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ class FileCache implements EngineInterface {
!fs.existsSync(path) && fs.mkdirSync(path, { recursive: true })
}

private write(key: string, data: any) {
private async write(key: string, data: any) {
this.ensureCacheDirectoryExists(this.path())

fs.writeFileSync(this.path() + '/' + '_' + this.hashKey(key), data, 'binary')
fs.writeFileSync(this.path() + '/' + '_' + (await this.hashKey(key) ), data, 'binary')
}

private writeWithTime(key: string, data: any, duration: number) {
private async writeWithTime(key: string, data: any, duration: number) {
this.ensureCacheDirectoryExists(this.path())
const currentDate = new Date()
const futureDate = new Date(currentDate.getTime() + duration * 60000)
Expand All @@ -52,15 +52,15 @@ class FileCache implements EngineInterface {
newArr['data'] = data

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

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

if (data['meta']) {
Expand Down Expand Up @@ -107,20 +107,20 @@ class FileCache implements EngineInterface {
return this.deleteFIle(key)
}

private hashKey(key: string) {
private async hashKey(key: string) {
return this.hash.make(key + '')
}

private async isCacheExist(key: string): Promise<Boolean> {
const path = this.path()
return fs.existsSync(path + '/' + this.hashKey(key) + '.cache')
return fs.existsSync(path + '/' + ( await this.hashKey(key) ) + '.cache')
}

private async deleteFIle(key: string): Promise<Boolean> {
try {
if (await this.isCacheExist(key)) {
const path = this.path()
fs.unlinkSync(path + '/' + this.hashKey(key) + '.cache')
fs.unlinkSync(path + '/' + ( await this.hashKey(key) ) + '.cache')
return true
}
return false
Expand Down
4 changes: 2 additions & 2 deletions src/Engines/RedisCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class RedisCache implements EngineInterface {
public async set(name: string, data: any, duration: number): Promise<any> {
if (name && data) {
if (duration === 0) {
return await this._addCache(name, data)
return this._addCache(name, data)
}
return await this._addExpiredCache(name, data, duration)
return this._addExpiredCache(name, data, duration)
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Services/DatabaseCacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DatabaseCacheService extends DatabaseCache implements ServiceInterface {
public async set(name: string, data: any, duration: number): Promise<any> {
if (name && data) {
data = JSON.stringify(data)
return await super.set(name, data, duration)
return super.set(name, data, duration)
// if (duration == null) {
// return await this._addCache(name, data);
// }
Expand All @@ -44,13 +44,13 @@ class DatabaseCacheService extends DatabaseCache implements ServiceInterface {

public async update(name: string, data: any, duration: number): Promise<any> {
if (await this.delete(name)) {
return await this.set(name, data, duration)
} else return await this.set(name, data, duration)
return this.set(name, data, duration)
} else return this.set(name, data, duration)
}

public async remember(name: string, duration: number, callback: Function): Promise<any> {
if (await this.has(name)) {
return await this.get(name)
return this.get(name)
} else {
const data = await callback()
await this.set(name, data, duration)
Expand All @@ -60,7 +60,7 @@ class DatabaseCacheService extends DatabaseCache implements ServiceInterface {

public async rememberForever(name: string, callback: Function): Promise<any> {
if (await this.has(name)) {
return await this.get(name)
return this.get(name)
} else {
const data = await callback()
await this.set(name, data, 50000000000000)
Expand Down
8 changes: 4 additions & 4 deletions src/Services/FileCacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FileCacheService extends FileCache implements ServiceInterface {

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

Expand All @@ -36,13 +36,13 @@ class FileCacheService extends FileCache implements ServiceInterface {
}

public async flush(): Promise<void> {
return await super.flush()
return 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)
} else return await this.set(name, data, duration)
return this.set(name, data, duration)
} else return this.set(name, data, duration)
}

private serialize(data: any): any {
Expand Down
10 changes: 5 additions & 5 deletions src/Services/MemCacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MemCacheService extends MemCache implements ServiceInterface {
public async set(name: string, data: any, duration: number): Promise<any> {
if (name && data) {
data = JSON.stringify(data)
return await super.set(name, data, duration)
return super.set(name, data, duration)
// if (duration == null) {
// return await this._addCache(name, data);
// }
Expand All @@ -44,13 +44,13 @@ class MemCacheService extends MemCache implements ServiceInterface {

public async update(name: string, data: any, duration: number): Promise<any> {
if (await this.delete(name)) {
return await this.set(name, data, duration)
} else return await this.set(name, data, duration)
return this.set(name, data, duration)
} else return this.set(name, data, duration)
}

public async remember(name: string, duration: number, callback: Function): Promise<any> {
if (await this.has(name)) {
return await this.get(name)
return this.get(name)
} else {
const data = await callback()
await this.set(name, data, duration)
Expand All @@ -60,7 +60,7 @@ class MemCacheService extends MemCache implements ServiceInterface {

public async rememberForever(name: string, callback: Function): Promise<any> {
if (await this.has(name)) {
return await this.get(name)
return this.get(name)
} else {
const data = await callback()
await this.set(name, data, 50000000000000)
Expand Down
8 changes: 4 additions & 4 deletions src/Services/RedisCacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ class RedisCacheService extends RedisCache implements ServiceInterface {

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

public async delete(name: string): Promise<Boolean> {
return await super.delete(name)
return super.delete(name)
}

public async update(name: string, data: any, duration: number): Promise<any> {
if (await this.delete(name)) {
return await this.set(name, data, duration)
} else return await this.set(name, data, duration)
return this.set(name, data, duration)
} else return this.set(name, data, duration)
}

public async flush(): Promise<void> {
Expand Down