【salesforce技术积累】-缓存的使用

发布时间 2023-08-03 14:58:25作者: 阿飞的尛蝴蝶

1.salesforce中的缓存可以分为 组织缓存 和 Session缓存。想要使用 缓存的话需要至少给他事先分出一块区域(有点想给电脑的硬盘分区的概念)。可以给分好的区域取名字,从而可以用APEX代码对其进行操作。

各块分区里面都会有一个组织缓存和一个Session缓存,而且分区的时候往往都十分5 MB 以上的空间。

・分区的具体步骤

 2.缓存的使用

①:缓存的形式

Namespace.Partition.Key

  「Namespace」: 组织的命名空间,可以用特殊的名字「local」来代替。

         「Partition」: 分区的名字。

          「Key」:想要保存的数据的名字,是唯一的。

例子:

Cache.Org.put('local.CurrencyCache.DollarToEuroRate', '0.91');

  如果分区是摩恩分区的话,可以简写

Cache.Org.put('DollarToEuroRate', '0.91');

3.缓存数据的添加与取得

  组织缓存的添加数据:

Cache.Org.getPartition()函数获取对应的分区,然后用put()函数去添加键和值。

  组织缓存获取数据:

分区对象.get('KEY')
// Get partition
Cache.OrgPartition orgPart = Cache.Org.getPartition('local.CurrencyCache');
// Add cache value to the partition. Usually, the value is obtained from a 
// callout, but hardcoding it in this example for simplicity.
orgPart.put('DollarToEuroRate', '0.91');
// Retrieve cache value from the partition
String cachedRate = (String)orgPart.get('DollarToEuroRate');

session缓存的数据添加与获取

// Get partition
Cache.SessionPartition sessionPart = Cache.Session.getPartition('local.CurrencyCache');
// Add cache value to the partition
sessionPart.put('FavoriteCurrency', 'JPY');
// Retrieve cache value from the partition
String cachedRate = (String)sessionPart.get('FavoriteCurrency');

4.缓存的有效时长

session缓存:最大8小时

组织缓存:默认24小时,最大48小时

5.在Visualforce中可以使用gorobal变量来访问缓存中的数据

使用  $Cache.Session 变量 或 Cache.Org变量

 

  

.get