| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- /*
- package com.qingjian.common.cache;
- import com.fasterxml.jackson.core.type.TypeReference;
- import com.qingjian.App;
- import com.qingjian.common.rocksdb.DefaultRocksDBStorage;
- import com.qingjian.common.utils.Json;
- import org.junit.jupiter.api.AfterEach;
- import org.junit.jupiter.api.Test;
- import org.noear.solon.test.SolonTest;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.List;
- import java.util.UUID;
- import static org.junit.jupiter.api.Assertions.*;
- @SolonTest(App.class)
- public class RocksDBCacheTest {
- private DefaultRocksDBStorage storage;
- private Path dbPath;
- @AfterEach
- void tearDown() throws IOException {
- if (storage != null) {
- storage.shutdown();
- }
- if (dbPath != null) {
- deleteDirectory(dbPath);
- }
- }
- @Test
- void shouldStoreAndReadGenericValue() throws IOException {
- RocksDBCache cacheService = newCacheService(30);
- List<String> expected = List.of("a", "b", "c");
- cacheService.store("list-key", expected, 60);
- List<String> actual = cacheService.get("list-key", new TypeReference<List<String>>() {
- }.getType());
- assertEquals(expected, actual);
- }
- @Test
- void shouldRemoveCachedValue() throws IOException {
- RocksDBCache cacheService = newCacheService(30);
- cacheService.store("remove-key", "value", 60);
- cacheService.remove("remove-key");
- assertNull(cacheService.get("remove-key", String.class));
- }
- @Test
- void shouldExpireCachedValueWithDefaultSeconds() throws Exception {
- RocksDBCache cacheService = newCacheService(1);
- cacheService.store("expire-key", "value", 0);
- Thread.sleep(1200L);
- assertNull(cacheService.get("expire-key", String.class));
- }
- private RocksDBCache newCacheService(int defaultSeconds) throws IOException {
- dbPath = Path.of("target", "rocksdb-cache-tests", UUID.randomUUID().toString());
- Files.createDirectories(dbPath);
- storage = new DefaultRocksDBStorage(dbPath.toString());
- assertTrue(storage.start());
- return new RocksDBCache(Json.objectMapper(), () -> storage);
- }
- private void deleteDirectory(Path path) throws IOException {
- if (Files.notExists(path)) {
- return;
- }
- try (var stream = Files.walk(path)) {
- stream.sorted((left, right) -> right.getNameCount() - left.getNameCount())
- .forEach(current -> {
- try {
- Files.deleteIfExists(current);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- });
- } catch (RuntimeException e) {
- if (e.getCause() instanceof IOException ioException) {
- throw ioException;
- }
- throw e;
- }
- }
- }
- */
|