-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathBookCache.cs
More file actions
26 lines (24 loc) · 767 Bytes
/
BookCache.cs
File metadata and controls
26 lines (24 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class BookCache
{
private Dictionary<int, Book> _cache = new Dictionary<int, Book>();
public Book GetBook(int isbn)
{
if (_cache.ContainsKey(isbn))
{
return _cache[isbn].Clone();
}
else
{
// Simulate fetching the book from a database
Book book = FetchBookFromDatabase(isbn);
_cache.Add(isbn, book);
return book.Clone(); // Return a clone to ensure the original object remains unaltered
}
}
private Book FetchBookFromDatabase(int isbn)
{
// Simulate database access
Console.WriteLine($"Fetching book with ISBN {isbn} from database...");
return new Book("Example Title", "Author Name", isbn);
}
}