📦 msgpack
MessagePack Binary Serialization - Kompakt ve Hızlı
~500 satır
~40 fonksiyon
30-50% Daha Küçük
📖 Genel Bakış
MessagePack, JSON'dan %30-50 daha küçük ve 2-3x daha hızlı binary serialization formatıdır. Network protokolleri, cache sistemleri ve IPC için idealdir.
🔑 Temel Özellikler
- JSON'dan %30-50 daha küçük boyut
- 2-3x daha hızlı serialization/deserialization
- Type-safe encoding/decoding
- Integers, floats, strings, binary, arrays, maps
- Nil/null value support
- Extension types
- Streaming API
- Zero-copy where possible
🚀 Hızlı Başlangıç
içe_aktar msgpack
// Serialize
değişken data = yap
name: "Alice",
age: 30,
scores: [95, 87, 92]
son
değişken bytes = msgpack.encode(data)
io.println("Encoded: {} bytes", bytes.uzunluk())
// Deserialize
değişken decoded = msgpack.decode(bytes)?
io.println("Name: {}", decoded["name"])
💡 Örnek: Redis-Style Cache with MessagePack
içe_aktar msgpack, redis, time
yapı CacheEntry[T] yap
value: T,
expires_at: time.Instant
son
yapı MessagePackCache yap
redis: redis.Client
son
fonksiyon cache_new(redis_url: yazı) -> Sonuç[MessagePackCache, Hata] yap
dön Tamam(MessagePackCache yap
redis: redis.connect(redis_url)?
son)
son
fonksiyon cache_set[T](cache: MessagePackCache, key: yazı, value: T, ttl_secs: sayı)
-> Sonuç[Hiçbir, Hata] yap
// Wrap with expiry
değişken entry = CacheEntry yap
value: value,
expires_at: time.now() + time.seconds(ttl_secs)
son
// Serialize with MessagePack (much smaller than JSON)
değişken bytes = msgpack.encode(entry)
// Store in Redis
cache.redis.setex(key, ttl_secs, bytes)?
dön Tamam(Hiçbir)
son
fonksiyon cache_get[T](cache: MessagePackCache, key: yazı)
-> Sonuç[İsteğe_Bağlı[T], Hata] yap
// Get from Redis
değişken bytes = cache.redis.get(key)?
eğer bytes.is_none() ise yap
dön Tamam(Hiçbir)
son
// Deserialize
değişken entry: CacheEntry[T] = msgpack.decode(bytes.unwrap())?
// Check expiry
eğer time.now() > entry.expires_at ise yap
cache.redis.del(key)?
dön Tamam(Hiçbir)
son
dön Tamam(Bazı(entry.value))
son
// Kullanım
fonksiyon ana() yap
değişken cache = cache_new("redis://localhost:6379")?
// Store user data
değişken user = yap
id: 123,
name: "Alice",
email: "alice@example.com",
roles: ["admin", "user"]
son
cache_set(cache, "user:123", user, 3600)? // 1 hour TTL
// Retrieve
değişken cached_user = cache_get(cache, "user:123")?
eğer cached_user.is_some() ise yap
io.println("Cache hit: {}", cached_user.unwrap().name)
son
son
📊 Boyut Karşılaştırması
// Test data
değişken data = yap
users: [
yap id: 1, name: "Alice", age: 30, active: doğru son,
yap id: 2, name: "Bob", age: 25, active: doğru son,
yap id: 3, name: "Charlie", age: 35, active: yanlış son
]
son
// JSON: 189 bytes
değişken json_bytes = json.encode(data)
io.println("JSON: {} bytes", json_bytes.uzunluk())
// MessagePack: 98 bytes (48% smaller!)
değişken msgpack_bytes = msgpack.encode(data)
io.println("MessagePack: {} bytes", msgpack_bytes.uzunluk())
io.println("Savings: {}%", (1.0 - msgpack_bytes.uzunluk().kesir() / json_bytes.uzunluk().kesir()) * 100.0)
🔗 İlgili Modüller
json- JSON serializationredis- Redis cachenetwork- Binary protocols