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

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

  get(key: K): V | undefined {
    const entry = this.cache[key];
    if (entry && entry.timestamp > Date.now() - this.capacity * 1000) {
      // Update timestamp and return value
      entry.timestamp = Date.now();
      return entry.value;
    }
    return undefined;
  }

  set(key: K, value: V): void {
    const entry = this.cache[key];
    if (entry) {
      // Update value while keeping the timestamp up-to-date
      entry.value = value;
      entry.timestamp = Date.now();
    } else {
      // Insert new entry
      this.cache[key] = { value, timestamp: Date.now() };
      if (Object.keys(this.cache).length > this.capacity) {
        const oldestKey = Object.entries(this.cache).reduce(
          (prev, curr) => (curr[1].timestamp < prev[1].timestamp ? curr : prev),
          [0, {}]
        );
        delete this.cache[oldestKey[0]];
      }
    }
  }

  get size(): number {
    return Object.keys(this.cache).length;
  }
}
```