1# 获取最近访问列表场景 2<!--Kit: ArkTS--> 3<!--Subsystem: CommonLibrary--> 4<!--Owner: @lijiamin2025--> 5<!--Designer: @weng-changcheng--> 6<!--Tester: @kirl75; @zsw_zhushiwei--> 7<!--Adviser: @ge-yafang--> 8 9为了快速访问最近使用的[Sendable](arkts-sendable.md)对象,从API version 18开始,ArkTS引入了[SendableLruCache](../reference/apis-arkts/arkts-apis-arkts-utils-SendableLruCache.md)。开发者可以通过向SendableLruCache实例中添加、删除和获取Sendable对象,实现快速访问最近使用的Sendable对象。本文提供使用SendableLruCache实现获取最近使用列表的开发指导,以书架为例,每次打开一本图书后,需将图书信息更新到最近访问列表中,并在下次访问书架页面时显示最近访问的图书列表。 10 11> **说明:** 12> 13> 使用SendableLruCache实例对象时需加锁,避免多线程同时操作导致数据不一致。 14> 存放到SendableLruCache实例中的对象必须是Sendable对象。 15 161. 创建SendableLruCache实例对象,并根据业务需求预设最大容量。<br/> 17 此例设置SendableLruCache实例的最大容量为4,用SendableClass类管理,并导出SendableClass类实例对象。 18 19 ```ts 20 // LruCache.ets 21 22 import { ArkTSUtils } from '@kit.ArkTS'; 23 24 // 使用use shared标记为共享模块。 25 "use shared" 26 27 // SendableClass实例对象在不同线程间可共享。 28 @Sendable 29 class SendableClass { 30 // 使用SendableLruCache实例对象时需加锁,避免多线程同时操作导致数据不一致。 31 lock_: ArkTSUtils.locks.AsyncLock = new ArkTSUtils.locks.AsyncLock(); 32 books_: ArkTSUtils.SendableLruCache<string, string> = new ArkTSUtils.SendableLruCache<string, string>(4); 33 34 constructor() { 35 this.books_.put("fourth", "Book4"); 36 this.books_.put("third", "Book3"); 37 this.books_.put("second", "Book2"); 38 this.books_.put("first", "Book1"); 39 } 40 41 // 封装put、get、keys方法,加锁操作。 42 public async put(key: string, value: string) { 43 await this.lock_.lockAsync(() => { 44 this.books_.put(key, value); 45 }) 46 } 47 48 public async get(key: string): Promise<string | undefined> { 49 return this.lock_.lockAsync(() => { 50 return this.books_.get(key); 51 }); 52 } 53 54 public async keys(): Promise<string[]> { 55 return this.lock_.lockAsync(() => { 56 return this.books_.keys(); 57 }); 58 } 59 } 60 61 export let lruCache = new SendableClass(); 62 ``` 63 642. 在Index.ets页面同目录下创建4个图书页面,每个页面显示相应的图书信息,并将每个页面的路径注册到`src/main/resources/base/profile/main_pages.json`文件中。 65 66 ```ts 67 // Book1.ets 68 69 @Entry 70 @Component 71 struct Index1 { 72 @State message: string = 'Hello World!'; 73 74 build() { 75 RelativeContainer() { 76 Text("第一本书的内容") 77 .id('first book') 78 .fontSize(20) 79 .padding(10) 80 .fontWeight(FontWeight.Bold) 81 .alignRules({ 82 center: { anchor: 'container', align: VerticalAlign.Center }, 83 middle: { anchor: 'container', align: HorizontalAlign.Center } 84 }) 85 Button("返回") 86 .fontSize(20) 87 .padding(10) 88 .fontWeight(FontWeight.Bold) 89 .position({ x: "50%" }) 90 .onClick(() => { 91 this.getUIContext().getRouter().pushUrl({ url: 'pages/Index' }); 92 }) 93 } 94 .height('100%') 95 .width('100%') 96 } 97 } 98 ``` 99 ```ts 100 // Book2.ets 101 102 @Entry 103 @Component 104 struct Index2 { 105 @State message: string = 'Hello World!'; 106 107 build() { 108 RelativeContainer() { 109 Text("第二本书的内容") 110 .id('second book') 111 .fontSize(20) 112 .padding(10) 113 .fontWeight(FontWeight.Bold) 114 .alignRules({ 115 center: { anchor: 'container', align: VerticalAlign.Center }, 116 middle: { anchor: 'container', align: HorizontalAlign.Center } 117 }) 118 Button("返回") 119 .fontSize(20) 120 .padding(10) 121 .fontWeight(FontWeight.Bold) 122 .position({ x: "50%" }) 123 .onClick(() => { 124 this.getUIContext().getRouter().pushUrl({ url: 'pages/Index' }); 125 }) 126 } 127 .height('100%') 128 .width('100%') 129 } 130 } 131 ``` 132 ```ts 133 // Book3.ets 134 135 @Entry 136 @Component 137 struct Index3 { 138 @State message: string = 'Hello World!'; 139 140 build() { 141 RelativeContainer() { 142 Text("第三本书的内容") 143 .id('third book') 144 .fontSize(20) 145 .padding(10) 146 .fontWeight(FontWeight.Bold) 147 .alignRules({ 148 center: { anchor: 'container', align: VerticalAlign.Center }, 149 middle: { anchor: 'container', align: HorizontalAlign.Center } 150 }) 151 Button("返回") 152 .fontSize(20) 153 .padding(10) 154 .fontWeight(FontWeight.Bold) 155 .position({ x: "50%" }) 156 .onClick(() => { 157 this.getUIContext().getRouter().pushUrl({ url: 'pages/Index' }); 158 }) 159 } 160 .height('100%') 161 .width('100%') 162 } 163 } 164 ``` 165 ```ts 166 // Book4.ets 167 168 @Entry 169 @Component 170 struct Index4 { 171 @State message: string = 'Hello World!'; 172 173 build() { 174 RelativeContainer() { 175 Text("第四本书的内容") 176 .id('fourth book') 177 .fontSize(20) 178 .padding(10) 179 .fontWeight(FontWeight.Bold) 180 .alignRules({ 181 center: { anchor: 'container', align: VerticalAlign.Center }, 182 middle: { anchor: 'container', align: HorizontalAlign.Center } 183 }) 184 Button("返回") 185 .fontSize(20) 186 .padding(10) 187 .fontWeight(FontWeight.Bold) 188 .position({ x: "50%" }) 189 .onClick(() => { 190 this.getUIContext().getRouter().pushUrl({ url: 'pages/Index' }); 191 }) 192 } 193 .height('100%') 194 .width('100%') 195 } 196 } 197 ``` 198 ```json 199 // main_pages.json 200 201 { 202 "src": [ 203 "pages/Index", 204 "pages/Book1", 205 "pages/Book2", 206 "pages/Book3", 207 "pages/Book4" 208 ] 209 } 210 ``` 211 2123. 访问书架页面时,自动展示最近访问的图书列表。 213 214 ```ts 215 // Index.ets 216 217 import { taskpool } from '@kit.ArkTS'; 218 import { lruCache } from './LruCache' 219 220 @Concurrent 221 async function updateBooks(key: string, value: string) { 222 // 在子线程更新最近访问列表 223 await lruCache.put(key, value); 224 } 225 226 @Entry 227 @Component 228 struct Index { 229 @State message: string = '书架'; 230 @State books: string[] = []; 231 232 async aboutToAppear () { 233 // 自动获取最近访问的图书列表 234 this.books = await lruCache.keys(); 235 } 236 237 build() { 238 Column({ space: 1 }) { 239 Text(this.message) 240 .id('HelloWorld') 241 .fontSize(50) 242 .fontWeight(FontWeight.Bold) 243 .alignRules({ 244 center: { anchor: 'container', align: VerticalAlign.Center }, 245 middle: { anchor: 'container', align: HorizontalAlign.Center } 246 }) 247 Button(this.books[3]) 248 .fontSize(20) 249 .padding(10) 250 .fontWeight(FontWeight.Bold) 251 .onClick(async () => { 252 // 获取绑定的图书信息 253 let value = await lruCache.get(this.books[3]); 254 // 更新最近访问列表 255 taskpool.execute(updateBooks, this.books[3], value); 256 this.getUIContext().getRouter().pushUrl({ url: 'pages/' + value }); 257 }) 258 Button(this.books[2]) 259 .fontSize(20) 260 .padding(10) 261 .fontWeight(FontWeight.Bold) 262 .onClick(async () => { 263 // 获取绑定的图书信息 264 let value = await lruCache.get(this.books[2]); 265 // 更新最近访问列表 266 taskpool.execute(updateBooks, this.books[2], value); 267 this.getUIContext().getRouter().pushUrl({ url: 'pages/' + value }); 268 }) 269 Button(this.books[1]) 270 .fontSize(20) 271 .padding(10) 272 .fontWeight(FontWeight.Bold) 273 .onClick(async () => { 274 // 获取绑定的图书信息 275 let value = await lruCache.get(this.books[1]); 276 // 更新最近访问列表 277 taskpool.execute(updateBooks, this.books[1], value); 278 this.getUIContext().getRouter().pushUrl({ url: 'pages/' + value }); 279 }) 280 Button(this.books[0]) 281 .fontSize(20) 282 .padding(10) 283 .fontWeight(FontWeight.Bold) 284 .onClick(async () => { 285 // 获取绑定的图书信息 286 let value = await lruCache.get(this.books[0]); 287 // 更新最近访问列表 288 taskpool.execute(updateBooks, this.books[0], value); 289 this.getUIContext().getRouter().pushUrl({ url: 'pages/' + value }); 290 }) 291 } 292 .height('100%') 293 .width('100%') 294 } 295 } 296 ```