modx xpdo is taking a long time to refresh data

210 Views Asked by At

I am using xpdo in MODX Revolution 2.7.3 to insert and extra data from custom tables in the same database as the modx installation. Everything works fine except that it takes about 20 minutes for changes in the tables to show up in the xpod query.

For example, I have a table of contacts with an adult field. The in a form I have a dropdown box where the options are the adult contact. It works fine but when you change the adult status of a contact the options in the dropdown takes 20 minutes to reflect the change.

See my code below

$class='Contacts';

$fields = array_keys($modx->getFields($class));

$collections = $modx->getCollection($class);

foreach($collections as $collection) {

    if($collection->get($fields[4])=='YES'){
    $output .= '<option value=' . $collection->get($fields[0]).'>'.$collection->get($fields[1])." ".$collection->get($fields[2]).'</option>';
    }

}


return $output;

There is only one table involved and the code for creating the table is:

    CREATE TABLE `cbdb_contacts` (
  `contactID` int(11) NOT NULL,
  `firstname` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
  `lastname` varchar(25) COLLATE utf8_unicode_ci NOT NULL,
  `dob` date NOT NULL,
  `adult` enum('YES','NO') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'NO',
  `mobile` text COLLATE utf8_unicode_ci NOT NULL,
  `landline` text COLLATE utf8_unicode_ci NOT NULL,
  `address` text COLLATE utf8_unicode_ci NOT NULL,
  `email` text COLLATE utf8_unicode_ci NOT NULL,
  `comments` longtext COLLATE utf8_unicode_ci NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

    -- Dumping data for table `cbdb_contacts`
--

INSERT INTO `cbdb_contacts` (`contactID`, `firstname`, `lastname`, `dob`, `adult`, `mobile`, `landline`, `address`, `email`, `comments`, `timestamp`) VALUES
(38, 'Tex', 'Brown', '2020-06-01', 'NO', '', '', '', '', '', '2020-07-12 18:34:19'),
(39, 'Mary', 'Brown', '2020-06-01', 'YES', '', '', '', '', '', '2020-07-06 19:03:23'),
(40, 'Pamela', 'Brown', '2020-06-01', 'YES', '', '', '', '', '', '2020-07-08 08:13:11'),
(41, 'Eddy', 'Green', '2020-06-01', 'NO', '', '', '', '', '', '2020-07-06 19:04:19'),
(42, 'Sheila', 'White', '2020-06-01', 'NO', '', '', '', '', '', '2020-07-12 18:54:03'),
(43, 'Dan', 'Black', '2020-06-01', 'NO', '', '', '', '', '', '2020-07-08 08:20:25'),
(134, 'Annete', 'Pray', '0000-00-00', 'NO', '', '', '', '', '', '2020-07-12 19:23:02'),
(133, 'Alex', 'Grey', '0000-00-00', 'NO', '', '', '', '', '', '2020-07-12 19:10:14'),
(132, 'Princess', 'Brown', '0000-00-00', 'NO', '', '', '', '', '', '2020-07-11 22:43:55'),
(131, 'Prince', 'Black', '0000-00-00', 'NO', '', '', '', '', '', '2020-07-11 22:39:22'),
(129, 'Tom', 'Smith', '0000-00-00', 'YES', '', '', '', '', '', '2020-07-11 22:34:32'),
(128, 'James', 'Dean', '0000-00-00', 'YES', '', '', '', '', '', '2020-07-11 22:14:19'),
(127, 'Peter', 'Paul', '0000-00-00', 'NO', '', '', '', '', '', '2020-07-11 22:08:52'),
(130, 'Tess', 'Logan', '0000-00-00', 'NO', '', '', '', '', '', '2020-07-11 22:38:35');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `cbdb_contacts`
--
ALTER TABLE `cbdb_contacts`
  ADD PRIMARY KEY (`contactID`),
  ADD KEY `firstname` (`firstname`) USING BTREE;

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `cbdb_contacts`
--
ALTER TABLE `cbdb_contacts`
  MODIFY `contactID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=135;
COMMIT;
1

There are 1 best solutions below

12
On BEST ANSWER

It appears that caching is enabled by default when using getCollection(), try passing false as the third parameter.

$collections = $modx->getCollection($class, null, false);

You might also have to pass an empty array as the second parameter, the documentation is a little conflicting.

$collections = $modx->getCollection($class, [], false);

Edit

Apparently snippet results are also cached and don't hit the database even, unless they are called using the no-cached syntax, [[!tag]]. Using the ! directive should bypass the caching of this specific snippet which is okay since this is such as minimal database query.