Switch to unified view

a b/functions/adminfunc/eeg_cache.m
1
% EEG_CACHE - Store data in cache with hashcode
2
%
3
% >> [cache data] = eeg_cache(cache, hashcode, data);  
4
%
5
% Inputs:
6
%   cache      - Cell array containing cached data
7
%   hashcode   - MD5 hashcode
8
%
9
% Optional input:
10
%   data       - This can be anything
11
%
12
% Outputs:
13
%   cache      - Updated cache information
14
%   data       - Data corresponding to the hash code. If the hash code
15
%                is not found, this output is empty.
16
%
17
% Note: this script checks if the cache variable is larger than option_cachesize
18
% If it is the case, elements are removed until an acceptable size is
19
% reached.
20
%
21
% Examples
22
% cache = eeg_cache([], 'dyufisf8da0df', 3); % store 3 with hashcode dyufisf8da0df
23
% [cache data] = eeg_cache(cache, 'dyufisf8da0df'); % retrieve 3
24
%
25
% Authors:  Arnaud Delorme, SCCN, INC, UCSD, 2015
26
27
% Copyright (C) Arnaud Delorme, SCCN, INC, UCSD, June 07, 2007, arno@sccn.ucsd.edu
28
%
29
% This file is part of EEGLAB, see http://www.eeglab.org
30
% for the documentation and details.
31
%
32
% Redistribution and use in source and binary forms, with or without
33
% modification, are permitted provided that the following conditions are met:
34
%
35
% 1. Redistributions of source code must retain the above copyright notice,
36
% this list of conditions and the following disclaimer.
37
%
38
% 2. Redistributions in binary form must reproduce the above copyright notice,
39
% this list of conditions and the following disclaimer in the documentation
40
% and/or other materials provided with the distribution.
41
%
42
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
46
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
47
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
48
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
52
% THE POSSIBILITY OF SUCH DAMAGE.
53
54
function [cache data] = eeg_cache(cache, hashcode, data)
55
56
eeglab_options;
57
58
ind = [];
59
if ~isempty(cache)
60
    allHashCodes = { cache.hashcode };
61
    maxlen       = max(max(cellfun(@length, allHashCodes)), length(hashcode));
62
    ind = find(strncmp(hashcode, allHashCodes, maxlen));
63
end
64
    
65
if isempty(ind)
66
    if nargin > 2
67
        cache(end+1).hashcode = hashcode;
68
        cache(end  ).data     = data;
69
70
        % make sure the cache is not too large
71
        while getfield(whos('cache'), 'bytes') > option_cachesize*1000000
72
            cache(1) = [];
73
        end
74
    else
75
        data = [];
76
    end
77
else
78
    data = cache(ind).data;
79
end