| �������� ����������� |
- ����������� �������� � ������
- ������������� ��� ������ � ������������� �����������
- ��� ���������� ���� synchronized, blocking
- ����������� ��������� ���������� �������� LFU, LRU, FIFO
- ��� �������� �������� ����� �������������� ������� (strong)
��� ������(soft) ������
- ��������������� ��� BSD ���������
|
| ���������� � runtime
Java |
cache4j ����� �������������� � JDK 1.3, 1.4,
1.5
��� ������������� � JDK 1.3 ����� xml ������ (��������: Apache
xerces) |
| ��� ����� ��� �������������
cache4j ? |
- cache4j_?.jar
- ���� ������������
- xml ������ (���� ������������ JDK 1.3)
|
| �������� ����������
cache4j |
- CacheFactory ���� ����� ��������� ������������
����� (�������� ������������, ����������, ��������, ...)
- Cache ��������� ������� � �������� ����
- CacheConfig ������������ ����
- CacheInfo ������������� �������������� ����������
� ����
- ManagedCache ��������� ���������� �����
������������ � ������� javadoc ����� ����� �����.
|
| ���������� |
|
� cache4j ���������� ��������� ���������� �������
���������� ��������� ����:
- synchronized - � ���� ���������� ������ � ��������
��������������� �� ������ ����. ��� �������� ��� � �����
������ ������� ���������, �������� ��� ������� ������ ��
���� ����� ������ ���� �����.
- blocking - � ���� ���������� ������ � ��������
��������������� �� ������ ��������. ��� �������� ��� ������
����� ���������� � ����� �������� ������ ���� ��� �����
�������� ��������� ����� ���� �������� ��� ����� � ��� ��
��������. �������� ���� ���� ����� �������� ������ ��������
��� � ���� � ����� ����� ����� ��� ��������� �� ������ �����,
������� ���������� � ���� �� �������, ������� � ����� ��������
�� ���� ������� ���� ������ ����� �� �������� �����������
������ � ���.
- nocache - � ���� ���������� ����������� ��� ������.
��� ���������� ����� ���� ������� ����� ����������� �����
��������� (�������� ��� ���� ����� ��������� ������������������
���������� ��� ����)
|
| ���������� �������� |
|
������ ���� ��������� ������� ��� ����������
���� �� ���� ����� ������� ����� �� �������. ���������� ����������
���� �������� ���������� �������� � ����(max-size) �\��� �����
������ ���������� �����(max-memory-size). ����� ������ �������
����� ������� ������������ ���������� ��������(����������).
� cache4j ����������� ��������� ��������� ����������:
- lru - (Least Recently Used) ��������� ������ �
�������� ������ ����� ������ ���������
- lfu - (Least Frequently Used) ��������� ������
� �������� ���� ���������� ���������� ���������
- fifo - (First In First Out) ��������� ������ �������
������ ����� ��������� � ����
|
| ����� ���������� �������
� ���� |
|
���������� �������� � ���� ����� ��������������
�����������:
- ttl - ������������ ����� ����� ������� � ����
- idle - ������������ ����� ����������� ������� �
����. ������ ������ ��� ����� ����� �������.
���� � ����������� �� ���� ������� �����������
����� ����� ��� ����� ����������� �� ������������ null. �������
� ������� ����������� ����� ����� ��� ����� ����������� ������������
��������� ��������� �������.
|
| �������� �������� � ���� |
|
��� �������� �������� � ���� ����� ��������������
strong ��� soft ������.
- strong - ������ ��� ������� ������ �� java ������
- soft - ���� ��� ������ ������������ ������ � �������
���� ����� ������ ������ java.lang.ref.SoftReference. �����
��� ����� ? ���� ������ �� ���������� ������ �������� ������
����� java.lang.ref.SoftReference �� garbage collector �����
������� ����� ������, �� ������ � ��� ������ ���� ������������
��� ������ ��������� JVM. �� ���� ���� JVM ��������� ������
�� ��� ��������� ������ ������ �� garbage collector, �����
����� �������, ������� ��� ������� ��������� ������ �����
java.lang.ref.SoftReference � ���� ����� ����� ������ ��
������������ ������� ���������� java.lang.OutOfMemoryError.
����� ��������� ������� ����� � ���� ����� ������� �����������
��������� ���������� �������� � � ���� ����� �������������
���������� ����������� ������������ ��� ��������� ������.
|
| ���� ������������ |
|
<cache-factory>
<cache-factory> ��� �������� ��� � �����
������������. ��������� �������� ���� ������� � ������� ����.
| ������� |
�� ��������� |
�������� |
| clean-interval |
30 ������ |
������ ������� ���� �� ���������� ��������. ��������
����� ��������� �:
- ������������� (60000 = 1������)
- �������� (60s = 60 ������)
- ������� (1m = 1 ������)
- ����� (1h = 1 ���)
|
<cache>
��� <cache> ��������� ������������ ������
���������� ����. ��������� �������� ���� ������� � �������
����.
| ������� |
�� ��������� |
�������� |
| id |
������� ����������
��� ���������� |
������������� ����, ������ ���� ����������.
� CacheFactory �� ����� ������������ ��� ���� � ����������
���������������. |
| desc |
null |
�������� ���������� ����. |
| ttl |
0 - ��� ����������� |
������������ ����� ����� ������� �
���� (Time To Live). �������� ����� ��������� �:
- ������������� (60000 = 1������)
- �������� (60s = 60 ������)
- ������� (1m = 1 ������)
- ����� (1h = 1 ���)
|
| idle |
0 - ��� ����������� |
������������ ����� ����������� �������
� ���� (Idle Time). �������� ����� ��������� �:
- ������������� (60000 = 1������)
- �������� (60s = 60 ������)
- ������� (1m = 1 ������)
- ����� (1h = 1 ���)
|
| max-memory-size |
0 - ��� ����������� |
������������ ����� ������ ���������� ����� ���������
����(����). �������� ����� ��������� �:
- ������ (1024 = 1 ��������)
- ���������� (1k = 1 ��������)
- ���������� (1m = 1 ��������)
|
| max-size |
0 - ��� ����������� |
������������ ���������� �������� �
����. |
| type |
synchronized |
��� ����. ��������� ��������:
- blocking
- synchronized
- nocache
|
| algorithm |
lru |
�������� �������� �������� �� ����. ��������� ��������:
- lru (Least Recently Used)
- lfu (Least Frequently Used)
- fifo (First In First Out)
|
| reference |
strong |
��� ������ �� ������ ������������ � ����. ���������
��������:
- strong ������� ������
- soft ������ ������ (SoftReference)
|
|
| ������ ������������� |
|
������: �������� ����������� � DAO ��� ��������
"����" � "������" ����� ����� ������� ��� � ������� �����
������������ ��������� �������.
���������� �������� "������" ���������� ��������
100��., ������� ��� ������ ����� ������� � ���� ��� �����
���� �����������. ���������� � ������ ��������� �������� ��
��������, ������� ��� ��� ����� �������� ����� ����������
�� ����������� ������ �\��� �� ���������� ��������. �����
����� ��� �������� ��������� �������� � ���� ����� ������������
������ ������. ��������� ���������� �������� "����" ��� �
���������� ��������� �������� �� ����������, ������� ��� ��������
������ ��� �� ����� ������������ ��� � ������������� �� ����������
��� ����������� ������. ������� "����" ��������
������ �� ������ "������" ������� ����� ���������
"�����" �� ���� ����� �������� ������ �� ������,
��� ��� "������" ����� ���������.
| ���� ������������: |
<?xml version="1.0" encoding="Windows-1251"?>
<!--
| �������� �������� ���������� �������� : 30 ������
-->
<cache-factory clean-interval="30s">
<!--
| ��� ��� ��������� ��������
| ����� ����� �������� : 1 ���
| ����� ����������� �������� : 30 �����
| ���������� �������� : 1000
| ������������ ����� ������ : 50 ��������
| ��� ������ �� ������� : soft(������ ������)
-->
<cache id="tempObj" desc="��� ��� ��������� ��������"
ttl="1h"
idle="30m"
max-size="1000"
max-memory-size="50m"
reference="soft" />
<!--
| ��� ��� �������� "������"
-->
<cache id="currency" desc="��� ��� �������� ������"/>
<!--
| ��� ��� �������� "����"
| ���������� �������� : 1000
-->
<cache id="account" desc="��� ��� �������� ����"
max-size="1000" />
</cache-factory>
|
| �������� ������������: |
import net.sf.cache4j.CacheFactory;
import net.sf.cache4j.Cache;
import java.io.InputStream;
import java.io.FileInputStream;
//...
CacheFactory �acheFactory = CacheFactory.getInstance();
try {
InputStream in = new FileInputStream("config.xml");
�acheFactory.loadConfig(in);
} catch (CacheException ce){
// ...
}
//...
|
| ��������� ���������� ����: |
//...
Cache tempObjCache = CacheFactory.getInstance().getCache("tempObj");
Cache accountCache = CacheFactory.getInstance().getCache("account");
Cache currencyCache = CacheFactory.getInstance().getCache("currency");
//...
|
| �������� "������": |
//...
public Currency loadCurrency(String id) throws Exception {
Currency currency = null;
try {
//������� ������� ������ � ��������������� id �� ����
currency = (Currency)currencyCache.get(id);
} catch (CacheException ce) {
//...
throw new Exception(ce);
}
//���� ������ ������� �� ����
if (currency != null) {
return currency;
}
try {
//���� ������� �� ���� � ���� ��� ����� ���������� ��
//���� � ����� ��������� � ���
currency = loadCurrencyFromDb(id);
} finally {
try {
currencyCache.put(id, currency);
} catch (CacheException ce) {
throw new Exception(ce);
}
}
}
|
| �������� "�����": |
//...
public Account loadAccount(String number) throws Exception {
Account account = null;
try {
//������� ������� ���� � ��������������� number �� ����
account = (Account)accountCache.get(number);
} catch (CacheException ce) {
//...
throw new Exception(ce);
}
//���� ������ ������� �� ����
if (account != null) {
return updateAccount(account);
}
try {
//���� ������� �� ���� � ���� ��� ����� ����������
//�� ���� � ����� ��������� � ���
account = loadAccountFromDb(number);
} finally {
try {
accountCache.put(number, account);
} catch (CacheException ce) {
throw new Exception(ce);
}
}
}
public Account updateAccount(Account account) throws Exception {
//��������� ������ �� ������
account.setCurrency(loadCurrency(account.getCurrencyId()));
return account;
}
|
| ���������� "������": |
public Currency saveCurrency(Currency currency) throws Exception {
saveCurrencyInDB(currency);
try {
currencyCache.remove(currency.getId());
} catch (CacheException ce) {
throw new Exception(ce);
}
return currency;
}
|
| ���������� "�����": |
public Account saveAccount(Account account) throws Exception {
saveAccountInDB(account);
try {
accountCache.remove(account.getNumber());
} catch (CacheException ce) {
throw new Exception(ce);
}
return account;
}
|
|