Skip to content

Commit 2f7c538

Browse files
committed
closes #69 ChainLP: Cache control
1 parent 6d7303f commit 2f7c538

File tree

7 files changed

+179
-0
lines changed

7 files changed

+179
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.aventstack.chainlp.api.cache;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.DeleteMapping;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequestMapping("/cache")
12+
public class CacheController {
13+
14+
@Autowired
15+
private CacheService service;
16+
17+
@DeleteMapping
18+
public ResponseEntity<Void> clearAll() {
19+
service.clearAll();
20+
return ResponseEntity.ok().build();
21+
}
22+
23+
@DeleteMapping("/{name}")
24+
public ResponseEntity<Void> clear(@PathVariable final String name) {
25+
service.clearCache(name);
26+
return ResponseEntity.ok().build();
27+
}
28+
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.aventstack.chainlp.api.cache;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.cache.CacheManager;
6+
import org.springframework.stereotype.Service;
7+
8+
import java.util.Objects;
9+
10+
@Slf4j
11+
@Service
12+
public class CacheService {
13+
14+
@Autowired
15+
private CacheManager cache;
16+
17+
public void clearAll() {
18+
log.info("Clearing all caches");
19+
cache.getCacheNames().forEach(x -> Objects.requireNonNull(cache.getCache(x)).clear());
20+
log.info("All caches cleared");
21+
}
22+
23+
public void clearCache(final String name) {
24+
log.info("Clearing cache: {}", name);
25+
Objects.requireNonNull(cache.getCache(name)).clear();
26+
log.info("Cache cleared: {}", name);
27+
}
28+
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.aventstack.chainlp.scheduler;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.cache.CacheManager;
5+
import org.springframework.scheduling.annotation.Scheduled;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
public class CacheTask {
10+
11+
@Autowired
12+
private CacheManager cacheManager;
13+
14+
@Scheduled(cron="0 0 0 * * ?")
15+
public void reportCurrentTime() {
16+
cacheManager.getCacheNames().forEach(cacheManager::getCache);
17+
}
18+
19+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class Cache {
2+
3+
}

chainlp/frontend/chainlp/src/app/pages/settings/settings.component.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,43 @@ <h5>Theme</h5>
3030
</ng-template>
3131
</li>
3232
<li [ngbNavItem]="2">
33+
<button ngbNavLink>Cache</button>
34+
<ng-template ngbNavContent>
35+
<div class="row">
36+
<div class="col-3">
37+
<h5>Clear Individual Caches</h5>
38+
<div class="text-secondary">
39+
Clear individual caches in the app. This action cannot be undone.
40+
</div>
41+
</div>
42+
<div class="col-1"></div>
43+
<div class="col-5">
44+
<button class="btn btn-primary mb-1 me-1" type="button" (click)="clearCache('projects');clearCache('project');">Clear Projects Cache</button>
45+
<button class="btn btn-primary mb-1 me-1" type="button" (click)="clearCache('builds');clearCache('build');">Clear Builds Cache</button>
46+
<button class="btn btn-primary mb-1 me-1" type="button" (click)="clearCache('tests');clearCache('test');">Clear Tests Cache</button>
47+
<div class="mt-2">
48+
<span *ngIf="isCacheCleared" class="text-success">Cache cleared!</span>
49+
</div>
50+
</div>
51+
</div>
52+
<div class="row mt-5">
53+
<div class="col-3">
54+
<h5>Clear All Caches</h5>
55+
<div class="text-secondary">
56+
Clears all caches in the app. This action cannot be undone and may require additional time to rebuild the cache.
57+
</div>
58+
</div>
59+
<div class="col-1"></div>
60+
<div class="col-3">
61+
<button class="btn btn-danger" type="button" (click)="clearAllCaches()">Clear all</button>
62+
<div class="mt-2">
63+
<span *ngIf="areCachesCleared" class="text-success">All caches cleared!</span>
64+
</div>
65+
</div>
66+
</div>
67+
</ng-template>
68+
</li>
69+
<li [ngbNavItem]="3">
3370
<button ngbNavLink>Secrets</button>
3471
<ng-template ngbNavContent>
3572
<div class="row mb-5">

chainlp/frontend/chainlp/src/app/pages/settings/settings.component.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ThemeService } from '../../services/theme.service';
55
import { SecretsService } from '../../services/secrets.service';
66
import { Secret } from '../../model/secret.model';
77
import { ErrorHandlerService } from '../../services/error-handler.service';
8+
import { CacheService } from '../../services/cache.service';
89

910
@Component({
1011
selector: 'app-settings',
@@ -14,10 +15,14 @@ import { ErrorHandlerService } from '../../services/error-handler.service';
1415
export class SettingsComponent implements OnInit, OnDestroy {
1516

1617
private readonly _secrets$: Subject<any> = new Subject<any>();
18+
private readonly _cache$: Subject<any> = new Subject<any>();
19+
private readonly _caches$: Subject<any> = new Subject<any>();
1720

1821
error: any;
1922
active: number = 1;
2023
version: string = '0.0.8';
24+
isCacheCleared: boolean = false;
25+
areCachesCleared: boolean = false;
2126

2227
/* theme */
2328
themes: Theme[] = [
@@ -48,6 +53,7 @@ export class SettingsComponent implements OnInit, OnDestroy {
4853

4954
constructor(private themeService: ThemeService,
5055
private secretsService: SecretsService,
56+
private cacheService: CacheService,
5157
private errorService: ErrorHandlerService) { }
5258

5359
ngOnInit(): void {}
@@ -62,6 +68,38 @@ export class SettingsComponent implements OnInit, OnDestroy {
6268
this.themeService.setTheme(theme);
6369
}
6470

71+
clearCache(name: string) {
72+
this.cacheService.clear(name)
73+
.pipe(takeUntil(this._cache$))
74+
.subscribe({
75+
next: (response: any) => {
76+
this.isCacheCleared = true;
77+
setTimeout(() => {
78+
this.isCacheCleared = false;
79+
}, 2000);
80+
},
81+
error: (err) => {
82+
this.error = this.errorService.getError(err);
83+
}
84+
});
85+
}
86+
87+
clearAllCaches() {
88+
this.cacheService.clearAll()
89+
.pipe(takeUntil(this._caches$))
90+
.subscribe({
91+
next: (response: any) => {
92+
this.areCachesCleared = true;
93+
setTimeout(() => {
94+
this.areCachesCleared = false;
95+
}, 2000);
96+
},
97+
error: (err) => {
98+
this.error = this.errorService.getError(err);
99+
}
100+
});
101+
}
102+
65103
saveSecrets(obj: any) {
66104
if (typeof obj === 'object') {
67105
for (const key in obj) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Injectable } from '@angular/core';
2+
import { HttpClient } from '@angular/common/http';
3+
import { Observable } from 'rxjs';
4+
import { BaseService } from './base.service';
5+
import { Cache } from '../model/cache.model';
6+
7+
@Injectable({
8+
providedIn: 'root'
9+
})
10+
export class CacheService extends BaseService<Cache> {
11+
12+
constructor(http: HttpClient) {
13+
super('/cache', http);
14+
}
15+
16+
clear(name: string): Observable<any> {
17+
return this.http.delete(`${this._path}/${name}`);
18+
}
19+
20+
clearAll(): Observable<any> {
21+
return this.http.delete(`${this._path}`);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)