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