Basics

Önbellek

Giriş

Nylo Website v7, verileri verimli bir şekilde depolamak ve almak için dosya tabanlı bir önbellek sistemi sağlar. Önbellekleme, API yanıtları veya hesaplanmış sonuçlar gibi maliyetli verileri depolamak için kullanışlıdır.

import 'package:nylo_framework/nylo_framework.dart';

// Cache a value for 60 seconds
String? value = await cache().saveRemember("my_key", 60, () {
  return "Hello World";
});

// Retrieve the cached value
String? cached = await cache().get("my_key");

// Remove from cache
await cache().clear("my_key");

Süreli Kaydetme

Bir değeri süre sınırı ile önbellelememek için saveRemember kullanın:

String key = "user_profile";
int seconds = 300; // 5 minutes

Map<String, dynamic>? profile = await cache().saveRemember(key, seconds, () async {
  // This callback only runs on cache miss
  printInfo("Fetching from API...");
  return await api<UserApiService>((request) => request.getProfile());
});

Süre sınırı içindeki sonraki çağrılarda, callback çalıştırılmadan önbelleteki değer döndürülür.

Süresiz Kaydetme

Verileri süresiz olarak önbellelememek için saveForever kullanın:

String key = "app_config";

Map<String, dynamic>? config = await cache().saveForever(key, () async {
  return await api<ConfigApiService>((request) => request.getConfig());
});

Veriler, açıkça kaldırılana veya uygulamanın önbelleği temizlenene kadar önbellekte kalır.

Veri Alma

Önbelleteki bir değeri doğrudan alın:

// Retrieve cached value
String? value = await cache().get<String>("my_key");

// With type casting
Map<String, dynamic>? data = await cache().get<Map<String, dynamic>>("user_data");

// Returns null if not found or expired
if (value == null) {
  print("Cache miss or expired");
}

Önbelleteki öğenin süresi dolmuşsa, get() otomatik olarak kaldırır ve null döndürür.

Doğrudan Veri Depolama

Callback olmadan doğrudan bir değer depolamak için put kullanın:

// Store with expiration
await cache().put("session_token", "abc123", seconds: 3600);

// Store forever (no seconds parameter)
await cache().put("device_id", "xyz789");

Veri Silme

// Remove a single item
await cache().clear("my_key");

// Remove all cached items
await cache().flush();

Önbellek Kontrolü

// Check if a key exists
bool exists = await cache().has("my_key");

// Get all cached keys
List<String> keys = await cache().documents();

// Get total cache size in bytes
int sizeInBytes = await cache().size();
print("Cache size: ${sizeInBytes / 1024} KB");

API Yanıtlarını Önbellekleme

api() Yardımcısını Kullanma

API yanıtlarını doğrudan önbelleyin:

Map<String, dynamic>? response = await api<ApiService>(
  (request) => request.get("https://api.github.com/repos/nylo-core/nylo"),
  cacheDuration: const Duration(minutes: 5),
  cacheKey: "github_repo_info",
);

printInfo(response);

NyApiService Kullanma

API servis metotlarınızda önbelleklemeyi tanımlayın:

class ApiService extends NyApiService {

  Future<Map<String, dynamic>?> getRepoInfo() async {
    return await network(
      request: (request) => request.get("https://api.github.com/repos/nylo-core/nylo"),
      cacheKey: "github_repo_info",
      cacheDuration: const Duration(hours: 1),
    );
  }

  Future<List<User>?> getUsers() async {
    return await network<List<User>>(
      request: (request) => request.get("/users"),
      cacheKey: "users_list",
      cacheDuration: const Duration(minutes: 10),
    );
  }
}

Ardından metodu çağırın:

Map<String, dynamic>? repoInfo = await api<ApiService>(
  (request) => request.getRepoInfo()
);

Platform Desteği

Nylo Website'nun önbelleği dosya tabanlı depolama kullanır ve aşağıdaki platform desteğine sahiptir:

Platform Destek
iOS Tam destek
Android Tam destek
macOS Tam destek
Windows Tam destek
Linux Tam destek
Web Mevcut değil

Web platformunda önbellek sorunsuz bir şekilde devre dışı kalır — callback'ler her zaman çalıştırılır ve önbellekleme atlanır.

// Check if cache is available
if (cache().isAvailable) {
  // Use caching
} else {
  // Web platform - cache not available
}

API Referansı

Metotlar

Metot Açıklama
saveRemember<T>(key, seconds, callback) Bir değeri süre sınırı ile önbelleye kaydeder. Önbelleteki değeri veya callback sonucunu döndürür.
saveForever<T>(key, callback) Bir değeri süresiz olarak önbelleye kaydeder. Önbelleteki değeri veya callback sonucunu döndürür.
get<T>(key) Önbelleteki bir değeri alır. Bulunamazsa veya süresi dolmuşsa null döndürür.
put<T>(key, value, {seconds}) Bir değeri doğrudan depolar. İsteğe bağlı süre sınırı (saniye).
clear(key) Belirli bir önbellek öğesini kaldırır.
flush() Tüm önbellek öğelerini kaldırır.
has(key) Önbellekte bir anahtarın var olup olmadığını kontrol eder. bool döndürür.
documents() Tüm önbellek anahtarlarının listesini alır. List<String> döndürür.
size() Toplam önbellek boyutunu bayt cinsinden alır. int döndürür.

Özellikler

Özellik Tür Açıklama
isAvailable bool Önbelleklemenin mevcut platformda kullanılıp kullanılamayacağı.