import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'cache_item.dart';


class CacheItemAdapter<T> extends TypeAdapter<CacheItem<T>> {
  @override
  final typeId = 0; // 确保这个ID是唯一的

  @override
  CacheItem<T> read(BinaryReader reader) {
    final value = reader.read() as T;
    final hasExpiry = reader.readBool();
    final expiryTime = hasExpiry ? reader.readInt() : null;
    return CacheItem<T>(value, expiryTime);
  }

  @override
  void write(BinaryWriter writer, CacheItem<T> obj) {
    writer.write(obj.value);
    writer.writeBool(obj.expiryTime != null);
    if (obj.expiryTime != null) {
      writer.writeInt(obj.expiryTime!);
    }
  }
}
