```typescript
export class LruCache<K, V> {
    private capacity: number;
    private cache: Map<K, { value: V, timestamp: number }>;
    private oldestKey: K | null;

    constructor(capacity: number) {
        if (capacity <= 0) {
            throw new Error("Capacity must be a positive integer");
        }
        this.capacity = capacity;
        this.cache = new Map();
        this.oldestKey = null;
    }

    get(key: K): V | undefined {
        const entry = this.cache.get(key);
        if (entry) {
            entry.timestamp = Date.now();
            this.moveToFront(key);
            return entry.value;
        }
        return undefined;
    }

    set(key: K, value: V): void {
        if (this.cache.has(key)) {
            this.cache.set(key, { value, timestamp: Date.now() });
            this.moveToFront(key);
        } else {
            if (this.cache.size >= this.capacity) {
                const oldestKey = this.oldestKey;
                if (oldestKey !== null) {
                    this.cache.delete(oldestKey);
                }
            }
            this.cache.set(key, { value, timestamp: Date.now() });
            this.oldestKey = key;
        }
    }

    get size(): number {
        return this.cache.size;
    }

    private moveToFront(key: K): void {
        const entry = this.cache.get(key)!; // Safe since we know it exists
        if (key !== this.oldestKey) {
            this.cache.delete(key);
            this.cache.set(key, entry);
            this.oldestKey = key;
        }
    }
}
```