Untitled

 avatar
unknown
java
25 days ago
2.7 kB
3
Indexable
## 1. Requirement Analysis

### Actors:
1. **User** - Registers, searches for movies.
2. **System** - Manages movies, cache, and retrieval.

### Features:
- **Movie registration** (ID, title, genre, etc.).
- **Multi-level caching** (L1: user-specific, L2: global popular searches, primary store: in-memory database).
- **Efficient movie retrieval** based on search parameters.

### Assumptions:
- Users can search movies by title, genre, or year.
- Caching follows an **LRU eviction policy**.
- Database acts as a fallback for cache misses.

## 2. Class Design

### Core Classes:
```java
// Abstract class for cache levels
abstract class Cache {
    protected Cache nextLevel;
    abstract Movie get(String query);
    abstract void put(String query, Movie movie);
}

// L1 Cache - User Specific
class L1Cache extends Cache {
    private Map<String, Movie> cache = new LinkedHashMap<>(5, 0.75f, true);
    public Movie get(String query) {...}
    public void put(String query, Movie movie) {...}
}

// L2 Cache - Global Popular
class L2Cache extends Cache {
    private Map<String, Movie> cache = new LinkedHashMap<>(20, 0.75f, true);
    public Movie get(String query) {...}
    public void put(String query, Movie movie) {...}
}

// Movie Entity
class Movie {
    private String id;
    private String title;
    private String genre;
    private int releaseYear;
    private double rating;
    // Constructors, Getters, Setters
}

// User Entity
class User {
    private String userId;
    private String name;
    private L1Cache userCache;
    // Constructors, Getters, Setters
}
```

## 3. Database Design

### Tables:
**Movies Table:**
| movie_id | title | genre | release_year | rating |
|----------|-------|-------|--------------|--------|
| 1        | Inception | Sci-Fi | 2010 | 8.8 |

**Users Table:**
| user_id | name |
|---------|------|
| U1      | Alice |

Indexes on **movie_id, title, genre** for efficient search.

## 4. Microservices Design
- **MovieService** - Handles movie registration and retrieval.
- **CacheService** - Manages L1, L2 cache operations.
- **SearchService** - Searches movies via cache and DB.

## 5. API Design

### 1. Register Movie
**POST /movies**
```json
{
    "id": "M1",
    "title": "Inception",
    "genre": "Sci-Fi",
    "releaseYear": 2010,
    "rating": 8.8
}
```
Response: `201 Created`

### 2. Search Movie
**GET /movies?query=inception**
Response:
```json
{
    "id": "M1",
    "title": "Inception",
    "genre": "Sci-Fi",
    "releaseYear": 2010,
    "rating": 8.8
}
```

## Conclusion
- **Used Strategy Pattern** for cache implementation.
- **Optimized DB schema** with indexing.
- **Microservices division ensures scalability.**
- **API design follows RESTful principles.**

Leave a Comment