cache_lite/object.rs
1/*
2 * @filename: object.rs
3 * @description: Cache object implementation for cache-lite library
4 * @author: TaimWay <taimway@gmail.com>
5 *
6 * Copyright (C) 2026 TaimWay
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27use std::io;
28use std::path::{Path, PathBuf};
29use std::time::SystemTime;
30
31/// Represents an individual cache object with file operations
32pub struct CacheObject {
33 name: String,
34 path: PathBuf,
35 id: u32,
36 created_at: SystemTime
37}
38
39impl CacheObject {
40 /// Creates a new CacheObject
41 pub fn new(
42 name: String,
43 path: PathBuf,
44 id: u32
45 ) -> Self {
46 let obj = CacheObject {
47 name,
48 path,
49 id,
50 created_at: SystemTime::now()
51 };
52
53 obj
54 }
55
56 /// Returns the cache object name
57 ///
58 /// # Returns
59 /// `&str` - Cache object identifier
60 pub fn name(&self) -> &str {
61 &self.name
62 }
63
64 /// Returns the filesystem path of the cache object
65 ///
66 /// # Returns
67 /// `&Path` - Path to cache file
68 pub fn path(&self) -> &Path {
69 &self.path
70 }
71
72 /// Returns the creation time of the cache object
73 ///
74 /// # Returns
75 /// `SystemTime` - Creation timestamp
76 pub fn created_at(&self) -> SystemTime {
77 self.created_at
78 }
79
80 /// Returns the cache object ID
81 ///
82 /// # Returns
83 /// `u32` - Unique identifier
84 pub fn id(&self) -> u32 {
85 self.id
86 }
87
88 /// Opens the cache file for reading/writing
89 ///
90 /// # Returns
91 /// `io::Result<std::fs::File>` - File handle or error
92 pub fn get_file(&self) -> io::Result<std::fs::File> {
93 std::fs::OpenOptions::new()
94 .read(true)
95 .write(true)
96 .create(true)
97 .open(&self.path)
98 }
99
100 /// Reads and returns the entire cache content as string
101 ///
102 /// # Returns
103 /// `io::Result<String>` - Cache content or error
104 pub fn get_string(&self) -> io::Result<String> {
105 std::fs::read_to_string(&self.path)
106 }
107
108 /// Writes string content to the cache file
109 ///
110 /// # Parameters
111 /// - `content: &str` - Content to write
112 ///
113 /// # Returns
114 /// `io::Result<()>` - Success or error
115 pub fn write_string(&self, content: &str) -> io::Result<()> {
116 std::fs::write(&self.path, content)
117 }
118
119 /// Deletes the cache object and its file
120 ///
121 /// # Returns
122 /// `io::Result<()>` - Success or error
123 pub fn delete(&self) -> io::Result<()> {
124 if self.path.exists() {
125 std::fs::remove_file(&self.path)?;
126 }
127 Ok(())
128 }
129
130 /// Checks if the cache has expired based on its lifecycle policy
131 ///
132 /// # Returns
133 /// `bool` - True if expired, false otherwise
134 #[deprecated(note="This enumeration has been deprecated due to issues, and it now only returns false")]
135 pub fn is_expired(&self) -> bool {
136 false
137 }
138}
139
140impl Clone for CacheObject {
141 fn clone(&self) -> Self {
142 CacheObject {
143 name: self.name.clone(),
144 path: self.path.clone(),
145 id: self.id,
146 created_at: self.created_at
147 }
148 }
149}